【问题标题】:Can't select dropdown option based on database data无法根据数据库数据选择下拉选项
【发布时间】:2015-12-31 12:22:19
【问题描述】:

我有三张桌子。

我需要从下拉列表中选择多个数据,并将该数据与表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>

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>

【问题讨论】:

  • &lt;?= $emailToRecord[$find] == $row['id'] ? ' selected="selected"' : ''; ?&gt; 不应该使用&lt;?php echo $email... 吗?请检查应选择的元素并将 HTML 粘贴到您的问题中
  • 另外,你使用的变量$find是在哪里定义的?
  • 您的$select2 查询返回空结果,因为HAVING COUNT(*) &gt; 1 条件不应该是HAVING COUNT(*) &gt;= 1 @samsam
  • 我建议确保您的条件为真 - $emailToRecord == $row['id'] ? ' 选定="选定"' : '';我想知道你是否在 $emailToRecord & $row['id'].. 中得到正确的结果

标签: php jquery mysql


【解决方案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");
}

【讨论】:

  • @sam sam 请检查我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2019-11-19
相关资源
最近更新 更多