我有房间清单
我根据用户每天添加的内容进行了显示记录。
如果用户添加新事件并使用其中一个房间。
示例:今天是2018年8月26日。预订了一个事件,它将显示ff。从数据库中检索。
豪华 -已预订1张
我要显示的是这个
豪华 -已预订1张
标准 -无
常规 -无
我想添加所有房间列表,即使某些房间在数据库中没有价值或 数据库中不存在。
但是我该怎么办呢? 请帮忙!
我的示例代码:
$connect = mysqli_connect("localhost", "root", "", "bookings");
$sql = "SELECT * FROM events WHERE (start BETWEEN '$start' AND '$end' OR
end BETWEEN '$start' AND '$end');
$result = mysqli_query($connect, $sql);
while($rows = mysqli_fetch_array($result))
{
echo $rows['room'];
}
答案 0 :(得分:0)
基于OP的评论,您应该使用GROUP BY子句获取每个类别的预订房间数。如果未找到特定房间类别的条目,则应让您的应用程序代码来处理。这是示例代码:
$connect = mysqli_connect("localhost", "root", "", "bookings");
$sql = "SELECT room, COUNT(id) as rooms_booked
FROM events
WHERE ((start BETWEEN '$start' AND '$end') OR
(end BETWEEN '$start' AND '$end')
)
GROUP BY room";
$result = mysqli_query($connect, $sql);
// all possible room categories
$all_room_categories = array('Deluxe', 'Standard', 'Regular');
// room categories for which there is some booking done
$booked_room_categories = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo $row['room'] . " - " . $row['rooms_booked'] . " booked" . "<\br>";
$booked_room_categories[] = $row['room'];
}
// Now find all the room categories for which no booking was found
$unbooked_room_categories = array_diff($all_room_categories, $booked_room_categories);
// Loop over unbooked rooms to print their status as well
foreach ($unbooked_room_categories as $cat)
{
echo $cat . " - " . " none" . "<\br>";
}