我正在创建一个会议室预订系统,该系统允许用户为单个和多个日期和时间段预订会议室。
我用示例数据创建了下表。
-------------------------------------------------------- roomno | dtfrm | dtto | tmfrm | tmto | -------------------------------------------------------- room1 | 2019-07-11 | 2019-07-11 | 07:00:00 | 10:00:00 | -------------------------------------------------------- room1 | 2019-07-12 | 2019-07-14 | 11:00:00 | 12:00:00 | -------------------------------------------------------- room1 | 2019-07-13 | 2019-07-13 | 10:00:00 | 11:00:00 | -------------------------------------------------------- room1 | 2019-07-14 | 2019-07-14 | 07:00:00 | 09:00:00 | --------------------------------------------------------
使用以下代码,我可以根据用户输入来打印房间繁忙的日期和时间。
// this is user input
$dfrm = "2019-07-11";
$dto = "2019-07-14";
$tmfrm = "07:00:00";
$tmto = "10:30:00";
$roomid = "room1";
$con = mysqli_connect($host,$user,$password,$database);
$result = $con->query( "SELECT * FROM booking WHERE dtfrm<='$dto' AND dtto>='$dfrm' AND roomno='$roomid' ORDER BY dtfrm" );
while($row = $result->fetch_assoc()) {
if($row['tmfrm']<$tmto && $row['tmto']>$tmfrm) {
echo "<br>Busy on: " . $row['dtfrm'] . " | " . $row['dtto'] . " | " . $row['tmfrm'] . " | " . $row['tmto'] ;
}
}
以下是输出:
Busy on: 2019-07-11 | 2019-07-11 | 07:00:00 | 10:00:00 Busy on: 2019-07-13 | 2019-07-13 | 10:00:00 | 11:00:00 Busy on: 2019-07-14 | 2019-07-14 | 07:00:00 | 09:00:00
在上面的代码中,我如何生成警报消息以告知用户他不能在指定的日期/时间预订房间,因为几个时隙不可用,并且还希望通知用户该房间可供预订它是。该消息应在代码执行后打印一次。我还想问一下是否需要提高代码的效率和准确性。