【发布时间】:2019-07-29 10:27:09
【问题描述】:
我必须分配一个任务,我必须处理以下事情: 制作一个数组“颜色”并用 10 个颜色名称填充它。 创建一个在表格中显示数组内容的脚本。 创建一个脚本,测试输入的颜色是否存在于颜色数组中,并返回是否存在。我现在的问题是我不知道如何在输入中返回正确答案。这是我的代码:
<?php
// different colors:
$color = array(
'#99ed5c' => 'green',
'#9c9991' => 'grey',
'#17ffb6' => 'blue',
'#a21647' => 'purple',
'#fbd8c1' => 'light-pink',
'#1f33a3' => 'dark-blue',
'#7e0509' => 'red',
'#f7d856' => 'yellow',
'#3b0b1b' => 'violet',
'#de4405' => 'orange'
);
function build_table($color){
// start table
$html = '<table>';
// header row
$html .= '<tr>';
foreach($color as $key=>$value){
$html .= '<th>' . htmlspecialchars($key) . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $color as $key=>$value) {
$html .= '<tr>';
$html .= '<td>' . htmlspecialchars($key) . '</td>';
$html .= '<td>' . htmlspecialchars($value) . '</td>';
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
echo build_table($color);
echo "fill in a color: <input type='text' name='kleur'>";
echo "<input type='submit'> <br>";
if (in_array("green", $color))
{
echo "Match found";
}
else
{
echo "Match not found";
}
【问题讨论】:
-
你想把它放在这个领域吗?
<input type='text' name='kleur'> -
是的。当我按下提交按钮时,如果找到匹配项或未找到匹配项,它必须显示图像。
-
如果您尝试获取提交的值,那么您可以在
echo "<form method='post'>fill in a color: <input type='text' name='kleur'>"; echo "<input type='submit'></form> <br>"; if (in_array($_POST['kleur'], $color)) { echo "Match found"; } else { echo "Match not found"; }之类的表单中包含文本字段
标签: php arrays forms foreach colors