好的,我们说我有一张下表:
CREATE TABLE booking (
idBooking INTEGER PRIMARY KEY,
bookingDate DATE,
roomId INTEGER,
startHour TIME,
endHour TIME
);
并且该表有3条记录: enter image description here
我使用框架codeigniter,这是我的观点:
<table border="1">
<thead>
<tr>
<th>Start Hour</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$open_time = "07:00:00";
$close_time = "15:00:00";
for( $i=$open_time; $i<$close_time; $i+=3600) { ?>
<tr>
<td >
<?php $timeloop = date('H:i:s', $i); echo $timeloop; ?> </h1>
</td>
<td>
<?php
$count = count($bookingList);
for ($j=0; $j < $count ; $j++) {
$endtime1 = strtotime($timeloop)+3600;
$endtime2 = date('H:i:s', $endtime1);
if ($bookingList[$j]->startHour == $timeloop ||$bookingList[$j]->endHour == $endtime2 ) {
?> <span>Booked </span> <?php
}
else { ?> <span> Available</span>
<?php
}
} ?>
</td>
</tr>
<?php } ?>
</tbody>
所以,我想根据预订表显示可用时间,这意味着表中没有记录。 这是我的期望:
07:00:00可用 08:00:00预订 09:00:00预订 10:00:00有空 11:00:00有空 12:00:00预订 13:00:00有空 14:00:00可用 15:00:00可用
但是,循环出了问题,我不知道如何定义逻辑,当表只有1条记录时,它工作得很好, 但是,当我插入另一条记录时,出现了问题,这里发生了什么
07:00:00有空可用 08:00:00预订可用 09:00:00已预订可用 10:00:00有现有供应 11:00:00有现有房源 12:00:00有空房预订 13:00:00有空房供应 14:00:00有空房供应 15:00:00有空可用
我非常感谢您的及时回复,我已经坚持了几天:(
答案 0 :(得分:0)
不要使用“for”语句来遍历数据库表。使用“while”,因为你知道记录的数量可以改变。
1)将逻辑传递给SQL查询; 2)用PHP执行查询;
例:
/* open th database connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
/* check if the connection was successful */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* query your database using the logic inside the query*/
$query = "SELECT * FROM booking WHERE openHour = '12:00' AND closeHour = '17:00'";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
?>
<td><?php echo $row['openHour']; ?><td>
<td><?php echo $row['closeHour']; ?><td>
<td>
<?php
if(date('H:m:i') == $row['openHour'] OR date('H:m:i') == $row['closeHour']){
echo "Booked";
}
?>
<td>
<?php
}
?>