【发布时间】:2016-05-28 11:34:28
【问题描述】:
我有 2 个执行房间搜索的 php 代码。用户指定否。床位、入住日期和退房日期。第一个代码检索具有相应床位数的所有房间。第二个代码检查房间是否有现有预订,如果有并且预订日期与显示房间的用户输入日期不冲突,如果没有现有预订,则仅显示。
我的第一个php代码:
$sql = "SELECT rooms.rid, beds, orientation, price FROM rooms
WHERE (beds = $nOfBeds)";
if ($rOrientation != "") {
$sql .= " AND orientation = '$rOrientation'";
}
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
我的第二个php代码:
<?php
while ($row = mysqli_fetch_array($results)) {
$sql2 = "SELECT rid, checkin, checkout FROM bookings";
$results2 = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
$row2 = mysqli_fetch_array($results2);
if ($row["rid"] == $row2["rid"]) {
if (($cInDate < $row2["checkin"] && $cOutDate <= $row2["checkin"]) ||
(($cInDate >= $row2["checkout"] && $cOutDate > $row2["checkout"]))) {
?>
<tr>
<td><?php echo $row["rid"]?></td>
<td><?php echo $row["beds"]?></td>
<td><?php echo $row["orientation"]?></td>
<td><?php echo $row["price"]?></td>
</tr>
<?php }
}
} ?>
目前它在过滤掉冲突日期方面工作正常,但它也在过滤掉没有现有预订的房间。我在使用此代码时遇到的另一个问题是,它似乎不适用于具有相同房间 id (rid) 的多个预订,所以现在它只过滤有关第一次预订的日期。
这是我的预订桌:http://imgur.com/DGOko1f
这是房间表:http://imgur.com/ED5KFES
【问题讨论】:
-
你可以用一个非常复杂的查询来完成它,使用连接并且不存在检查数据范围。
-
@splash58 你能详细说明一下吗?我尝试过使用 join 以及不同的 where 语句无济于事。
-
为什么你需要多行试试我的代码