我需要创建一个为期一天的议程,并预订我们的自修室。 该议程应显示所有房间并显示占用时间,如:
8:00 8:15 8:30 8:45 9:00 9:15 9:30 ...
Room 1 | [username ] [username 2
Room 2 | [username 3 ]
Room 3 |[username 4 ] [username 5 ...
...and so on
所以从表中可以清楚地看到,1号房间的占用时间为8:15-9:00和9:30 -...,Room2占用... 但也有哪些学习室是免费的。
我有点卡在了莫。
这是我已有的代码,但它不完整:
<table>
<tr>
<?php
$roomlist = "SELECT * FROM rooms";
$roomresult = mysql_query($roomlist);
while($roomnames=mysql_fetch_array($roomresult)) {
$roomid=$roomnames['roomid'];
$roomname=$roomnames['roomname'];
?>
<td><?= $roomname;?></td>
<?php
$query = "SELECT * FROM reservations WHERE roomid = '$roomid' AND (start LIKE '%" . $agenda . "%') ORDER BY start,roomid,end ";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
$startdate = explode(" ",$start);
$startdate[0] = explode("-",$startdate[0]);
$startdate[1] = explode(":",$startdate[1]);
$StartFormat = mktime($startdate[1][0],$startdate[1][1],$startdate[1][2],$startdate[0][1],$startdate[0][2],$startdate[0][0]);
$StartDate = date("d/m/Y",$StartFormat);
$StartTime = date("H:i",$StartFormat);
$stopdate = explode(" ",$end);
$stopdate[0] = explode("-",$stopdate[0]);
$stopdate[1] = explode(":",$stopdate[1]);
$StopFormat = mktime($stopdate[1][0],$stopdate[1][1],($stopdate[1][2]+1),$stopdate[0][1],$stopdate[0][2],$stopdate[0][0]);
$StopDate = date("d/m/Y",$StopFormat);
$StopTime = date("H:i",$StopFormat);
$cell = (strtotime($StopTime)-strtotime($StartTime))/(60*15); //a quarter per cell
?>
<td><?= $username . " " . $cell;?></td>
<?php } ?>
</tr>
<?php
}
mysql_close();
?>
</table>`
$cell
的计算是因为我想我需要知道预约需要多少个单元格(每季度一个),但我现在停留在如何计算空闲插槽,以及如何实现所有这些在桌子上。
更新: 表'预订'的结构
reservationid,start,end,roomid,username
41,2014-01-06 08:00:00.000000,2014-01-06 08:59:59.000000,28,stefdg
答案 0 :(得分:0)
作为SQL的基本示例,让您了解如何返回一系列可能的时段/房间组合,并将其与您的预订相结合
SELECT *
FROM rooms
CROSS JOIN
(
SELECT DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 8 HOUR), INTERVAL ((units.a + tens.a * 10) * 15) MINUTE) AS TimeSlotStart
FROM
(SELECT 0 AS a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units,
(SELECT 0 AS a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
WHERE units.a + tens.a * 10 <= 40
) TimeSlots
LEFT OUTER JOIN reservations ON rooms.roomid = reservations.roomid AND TimeSlots.TimeSlotStart >= start AND TimeSlots.TimeSlotStart < end
注意,不太确定您的最终价值适用于什么(即,如果预订在9:00结束,我假设与9:00开始的时间段无关)
编辑 - 基于您的报废输出详细信息表。这只是为房间占用的任何时间段推出xxx。
<html>
</html>
<body>
<table>
<?php
$link = mysqli_connect('localhost', 'root', '');
$db_selected = mysqli_select_db($link, 'test');
$sql = "SELECT a.timeslot_start, b.roomid, b.roomname, TIME(c.start) AS start
FROM timeslots a
CROSS JOIN rooms b
LEFT OUTER JOIN reservations c
ON b.roomid = c.roomid
AND a.timeslot_start >= TIME(c.start)
AND a.timeslot_start < TIME(c.end)
ORDER BY b.roomid, a.timeslot_start";
$roomresult = mysqli_query($link, $sql) or die(mysql_error());
while($roomnames=mysqli_fetch_assoc($roomresult))
{
$room_bookings[$roomnames['roomid']][] = $roomnames;
}
$first_room = reset($room_bookings);
echo "<table>";
$FirstRoomDetails = reset($room_bookings);
$Header = array(0=>array("<td></td>"), 1=>array("<td></td>"));
foreach($FirstRoomDetails AS $aRoomTimeSlot)
{
$TimeSlotSplit = explode(':', $aRoomTimeSlot['timeslot_start']);
if ($TimeSlotSplit[1] == '00')
{
$Header[0][] = "<td colspan='4'>".$TimeSlotSplit[0]."</td>";
}
$Header[1][] = "<td>".$TimeSlotSplit[1]."</td>";
}
echo "<tr>".implode('', $Header[0])."</tr>\r\n";
echo "<tr>".implode('', $Header[1])."</tr>\r\n";
foreach($room_bookings AS $aRoomsBookings)
{
echo "<tr>";
$First = true;
foreach($aRoomsBookings AS $aRoomTimeSlot)
{
if ($First)
{
$First = false;
echo "<td>".$aRoomTimeSlot['roomname']."</td>";
}
echo "<td>".(($aRoomTimeSlot['start'] == '') ? '' : 'xxxx')."</td>";
}
echo "</tr>\r\n";
}
echo "<table>";
mysqli_close($link);
?>
</table>
</body>
</html>