我需要创建一个一日游议程,其中要保留我们的自修室。
该议程应显示所有房间并显示占用时间,例如:

        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 -...,房间2的占用时间是...
而且还有哪些时间自习室是免费的。

而且我有点卡住了。

这是我已经拥有的代码,但是不完整:

<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

最佳答案

作为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>

关于php - 包含表格,PHP和MySQL的一天议程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21156583/

10-15 16:33