我试图在HTML选择输入中显示一组值的SQL行。
这是我的表格(行颜色是一个varchar类型):
| id | |色|
|12| Red, Green, Blue
|13|黄色,绿色14 | |黄色
它应该根据需要将值集合内爆!
<select>
<option>Red</option>
<option>Green</option>
<option>Blue</option>
</select>
我该怎么做呢?
"获取所有的颜色值,并以一个值显示它们中的每一个"
echo '<select>';
$results = mysql_query("SELECT colors FROM MyTable where id='$id'");
while($rows = mysql_fetch_assoc($results)) {
echo '<option>'.implode($rows['colors']).'</option>';
}
echo '</select>';
谢谢…
其他人说的是正确的,您在数据库中做的不是最好的方法。但是,如果需要使用这样的数据库,下面的代码应该会有所帮助:
echo '<select>';
$results = mysql_query("SELECT colors FROM MyTable where id='$id'");
while($row = mysql_fetch_assoc($results))
$colors = explode(",", $row['colors']);
$arr_length = count($colors);
for ($i=0; $i<$arr_length; $i++){
echo '<option>'.$colors[$i].'</option>';
}
}
echo '</select>';
试试这个:-
$results = mysql_query("SELECT colors FROM MyTable where id='$id'");
$option ='<select>';
while($rows = mysql_fetch_assoc($results)) {
$new = explode(",",$rows['colors']);
foreach ($new as $value) {
$option = $option . '<option>'. $value .'</option>';
}
}
$option = $option . '</select>';
echo $option;