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

时间:2015-12-31 12:22:20

标签: php jquery mysql

我有三张桌子。

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

如果下拉项目已经在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>

1 个答案:

答案 0 :(得分:-1)

当您拥有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");
}