我正在尝试select
用 PHP 填充一个字段。问题是我无法弄清楚如何显示它们,因为我得到了数据库中值匹配的那个出现两次,因为我将它作为选定的回显,然后循环它所有的结果。如何只显示selected
与字段值匹配的字段值,然后显示所有与所选字段值不匹配的字段?
表类别
cat_id cat_name
1 soccer
2 baseball
3 basketball
表文章
art_id art_cat_id
1 1
PHP/HTML
<select name="category">
<?php
$sql = "SELECT cat_id cat_name, art_id, art_cat_id
FROM categories LEFT JOIN articles
ON categories.cat_id = articles.art_cat_id
WHERE art_id = 1";
$result = query($sql);
if($result===false) {
echo("Query Fail");
}
else {
?>
<option value="<?php echo $data['art_cat_id'] ?>" selected="selected"><?php echo $data['cat_name'] ?></option>
<?php
while( $data = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $data['cat_id'] ?>"><?php echo $data['cat_name'] ?></option>
<?php
}
}
?>
</select>
它返回了什么
<select name="category">
<option value="1" selected="selected">soccer</option>
<option value="1">soccer</option>
<option value="2">baseball</option>
<option value="3">basketball</option>
</select>
我在寻找什么
<select name="category">
<option value="1" selected="selected">soccer</option>
<option value="2">baseball</option>
<option value="3">basketball</option>
</select>