我需要使用数据库中的日期填充下拉菜单,这是我到目前为止所做的
<div class="col-md-6">
<?php
$query_user_group = mysqli_prepare ($conn, "
SELECT group_name
, group_id
FROM user_group_join
LEFT
JOIN user_group
ON user_group_join . group_join_id = user_group . group_id
WHERE user_join_id = ?
");
mysqli_stmt_bind_param($query_user_group, 'i', $client_id);
mysqli_stmt_execute($query_user_group);
mysqli_stmt_bind_result($query_user_group, $group_name, $group_id);
mysqli_stmt_fetch ($query_user_group);
mysqli_stmt_close($query_user_group);
?>
<div class="form-group">
<label class="control-label">Condominio in gestione*</label>
<select class="bs-select form-control" name="usergroup">
<option value="<?php echo $group_id;?> " selected="selected"><?php echo $group_name;?></option>
<?php
$select_group_query= mysqli_prepare($conn, "SELECT group_id, group_name FROM user_group");
mysqli_stmt_execute($select_group_query);
mysqli_stmt_bind_result($select_group_query, $idgruppo, $nomegruppo);
while(mysqli_stmt_fetch($select_group_query))
{
echo "<option value= '".$idgruppo."'>" . $nomegruppo . "</option>";
}
?>
</select>
<span class="help-block"> Assicurati di aver creato una scheda condominio! <br>Per inserire un nuovo condominio <a href="admin_create_new_group.php">Clicca Qui</a></span>
</div>
</div>
问题是下拉列表在下拉列表中显示所选值两次,任何想法我如何排序? 这是错误的屏幕截图 enter image description here 非常感谢
答案 0 :(得分:0)
您可以在DISTINCT
查询中使用SELECT
,如下所示:
SELECT DISTINCT group_id, group_name FROM ...
此外,您根本不需要这一行,
<option value="<?php echo $group_id;?> " selected="selected"><?php echo $group_name;?></option>
您可以使用option标记的选定属性来获得所需的结果。
所以你的下拉列表代码应该是这样的:
<select class="bs-select form-control" name="usergroup">
<?php
$select_group_query= mysqli_prepare($conn, "SELECT DISTINCT group_id, group_name FROM user_group");
mysqli_stmt_execute($select_group_query);
mysqli_stmt_bind_result($select_group_query, $idgruppo, $nomegruppo);
while(mysqli_stmt_fetch($select_group_query)){
$output = "<option value= '".$idgruppo."'";
if($idgruppo == $group_id){
$output .= " selected='selected'";
}
$output .= ">" . $nomegruppo . "</option>";
echo $output;
}
?>
</select>