如何使用php中的while循环从以下下拉代码中选择特定项?
我可以填充下拉列表,但无法从下拉列表中选择特定项目。如果可以的话请帮忙。感谢。
<div class="form-group">
<select name="submit" for="no_id">
<?php
$query = "SELECT * FROM crap";
$select_dropdown = mysqli_query($conn, $query);
#function confirm stores in functions.php
confirm($select_dropdown);
while($row=mysqli_fetch_assoc($select_dropdown)){
$keyas_id= $row['keyas_id'];
$valueas_title =$row['valueas_title'];
echo "<option value=$keyas_id> $valueas_title </option>";
}
?>
</select>
</div>
答案 0 :(得分:0)
$selectedId = 'Your selected ID';
$isSelected = '';
if ( $keyas_id === $selectedId) {
$isSelected = "selected='selected'";
}
echo "<option value=$keyas_id $isSelected> $valueas_title </option>";
答案 1 :(得分:0)
您可以在选项中添加if条件,以便比较keyas_id
是否等于selected_id
,如果它相等,那么您可以选择以下内容:
<div class="form-group">
<select name="submit" for="no_id">
<?php
$query = "SELECT * FROM crap";
$select_dropdown = mysqli_query($conn, $query);
#function confirm stores in functions.php
confirm($select_dropdown);
$selectedId = 'Your selected ID';
while($row=mysqli_fetch_assoc($select_dropdown)){
$keyas_id= $row['keyas_id'];
$valueas_title =$row['valueas_title'];
echo "<option if($keyas_id==$selectedId) { selected='selected' } value=$keyas_id> $valueas_title </option>";
}
?>
</select>
</div>