gpt4 book ai didi

php - 无法根据数据库数据选择下拉选项

转载 作者:行者123 更新时间:2023-11-30 23:46:29 26 4
gpt4 key购买 nike

我有三个表。

我需要从下拉列表中选择多个数据,并使用 hotel_rooms 表中的酒店 id 列保存该数据。

如果下拉项已经在 hotel_rooms 表中,我需要选择该选项。

我可以在下拉列表中列出数据。但我的问题是,hotel_rooms 中已保存的数据未被选中。

请检查附件图片和给定的代码,帮助我解决这个问题。

hotel
-----
id name address
42 taj delhi

room
-----
id title
1 suit
2 deluxe
4 standard

hotel_rooms
-----------
hotel_id room_id
42 1
42 2
42 4

代码

<select class="form-control" multiple="multiple" name="example-basic" size="5">
<?php
$hotel_id = $_GET['editID']; // Hotel ID = 42
$emailToRecord = array();
$select2 = $mysqli->query("SELECT * FROM rooms WHERE id IN ( SELECT * FROM ( SELECT room_id FROM hotel_rooms WHERE hotel_id = '$hotel_id' GROUP BY room_id HAVING COUNT(*) > 1 ) AS subquery )");
while ($row2 = $select2->fetch_array()) {
$emailToRecord[$row2["id"]] = $row2;
}
$select = $mysqli->query("SELECT id, title FROM rooms ORDER BY id DESC");
while ($row = $select->fetch_array()) {
?>
<option class="hotelrooms" value="<?php echo $row['id']; ?>"<?= $emailToRecord == $row['id'] ? ' selected="selected"' : ''; ?>><?php echo $row['title']; ?></option>
<?php
}
?>
</select>

enter image description here

Firebug 检查结果

<li class=" hotelrooms">
<label class="ui-corner-all" title="" for="ui-multiselect-0-option-0">
<input id="ui-multiselect-0-option-0" type="checkbox" title="" value="31" name="multiselect_0">
<span>rooms</span>
</label>
</li>
<li class=" hotelrooms">
<label class="ui-corner-all" title="" for="ui-multiselect-0-option-1">
<input id="ui-multiselect-0-option-1" type="checkbox" title="" value="30" name="multiselect_0">
<span>Dummy</span>
</label>
</li>
<li class=" hotelrooms">
<label class="ui-corner-all" title="" for="ui-multiselect-0-option-2">
<input id="ui-multiselect-0-option-2" type="checkbox" title="" value="29" name="multiselect_0">
<span>Tests</span>
</label>
</li>
<li class=" hotelrooms">
<label class="ui-corner-all" title="" for="ui-multiselect-0-option-3">
<input id="ui-multiselect-0-option-3" type="checkbox" title="" value="28" name="multiselect_0">
<span>Superior Zimmer Einzelnutzung</span>
</label>
</li>

最佳答案

当你有multiple select你必须做一些事情

//take all saved it array to variable
var valArr = [101,102];
i = 0, size = valArr.length;
for(i; i < size; i++){
$("#data").multiselect("widget").find(":checkbox[value='"+valArr[i]+"']").attr("checked","checked");
$("#data option[value='" + valArr[i] + "']").attr("selected", 1);
$("#data").multiselect("refresh");
}

关于php - 无法根据数据库数据选择下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com