示例MySQL查询:返回的是两个值(Desert和Grasslands。)我想将这两个值与checkboxes
匹配为"checked"
其他值将保留unchecked
。
我的PHP结果集:
$row[biome];
<?php
while($row = mysqli_fetch_assoc($selectQueryBiomeResult)) {
echo '<input type="checkbox" name="biomeCheck[]" value="Desert"> Desert';
echo '<input type="checkbox" name="biomeCheck[]" value="Grasslands"> Grasslands';
echo '<input type="checkbox" name="biomeCheck[]" value="Deciduous Forest"> Deciduous Forest';
echo '<input type="checkbox" name="biomeCheck[]" value="Tropical Rainforest"> Tropical Rainforest';
}
?>
答案 0 :(得分:2)
提供示例代码 -
<html>
<?php
mysql_connect("host", "username", "pwd") or die("connect error");
mysql_select_db("db_name");
$res=mysql_query("write your query here");
$val_array=array();
while($row=mysql_fetch_array($res))
{
$val_array[]=$row[0];
}
?>
<body>
<input type="checkbox" name="biomeCheck[]" value="Desert" <?php if(in_array("Desert", $val_array)) echo "checked"?>> Desert;
<input type="checkbox" name="biomeCheck[]" value="Grasslands" <?php if(in_array("Grasslands", $val_array)) echo "checked"?>> Grasslands;
<input type="checkbox" name="biomeCheck[]" value="Deciduous Forest" <?php if(in_array("Deciduous Forest", $val_array)) echo "checked"?>> Deciduous Forest;
<input type="checkbox" name="biomeCheck[]" value="Tropical Rainforest" <?php if(in_array("Tropical Rainforest", $val_array)) echo "checked"?>> Tropical Rainforest;
</body>
</html>
答案 1 :(得分:1)
如果你的结果是数组,那么试试这个
示例数组
$my_array = array(
'first' => 'Desert',
'second' => 'Grasslands',
'third' => 'Deciduous Forest'
);
<input type="checkbox" name="vehicle" value="Desert" <?php if(in_array('Desert', $my_array)) echo( 'checked = "checked"'); ?>/> Desert<br />