我有一个带有下拉菜单的表单,该菜单由一个while语句填充,从mysql的数据库中提取一个数组。我试图为其中一个选项添加一个选定的属性,我将其存储在页面其他地方的变量$inv_item"type“中。
function find_all_types() {
global $connection;
$query = "SELECT type ";
$query .= "FROM types ";
$query .= "ORDER BY id ASC";
$type_set = mysqli_query($connection, $query);
confirm_query($type_set);
return $type_set;
}
<select name="type">
<?php
$type_set = find_all_types();
while ($type = mysqli_fetch_assoc($type_set)){
?>
<option value= "<?php echo $type['type'] ?>"><?php echo $type['type'] ?></option>
<?php } ?>
</select>我不确定是否有更好的方法来完成这一任务,或者我是否可以使用while语句使其工作。这是相当困难的研究,我找不到同样的情况,大多数搜索结果,人们填充他们的下拉菜单与个别写的选项。
我知道mysql命令是不受欢迎的,可以使用它。
发布于 2016-05-14 04:17:37
you could do it as a comparison in the option:
<option value= "<?php echo $type['type'] ?>" <?php if($type['type'] == $inv_item["type"]){echo" selected";}?>><?php echo $type['type'] ?></option>但是它看起来很混乱--在php里跳来跳去--我会呆在里面:
<?php
$type_set = find_all_types();
while ($type = mysqli_fetch_assoc($type_set)){
$type_item = $type['type'];
echo"<option value= '".$type_item."'";
if($type_item == $inv_item["type"]){echo" selected";}
echo" >".$type_item."</option>";
}
?>https://stackoverflow.com/questions/37222432
复制相似问题