大家好日子,根據預訂系統中的可用房間打印HTML表格
我試圖打印一個HTML表格;給定兩個日期(例如7月1日至7月31日),它將在標題上打印日期。然後,每一排將成爲一個房間。 然後,當某個房間在當天預訂時,單元格將具有紅色背景色,如果不是,則它將保持白色。
我在IF中迷路了,看看房間是否應該打印紅色。 到目前爲止,我得到這樣的輸出:http://jsfiddle.net/KatsuroKurosaki/kCkJD/
,我試圖實現這一目標之一:http://jsfiddle.net/KatsuroKurosaki/kCkJD/1/
基於數據庫查詢輸出:
SELECT idsRoom, checkin, checkout
FROM bookings
WHERE checkout >= '2014-07-01' AND checkin <= '2014-07-31'
+---------+------------+------------+
| idsRoom | checkin | checkout |
+---------+------------+------------+
| 2 | 2014-06-27 | 2014-07-02 |
| 4 | 2014-07-08 | 2014-07-09 |
| 6,7,8 | 2014-07-18 | 2014-07-22 |
| 14 | 2014-07-31 | 2014-08-02 |
+---------+------------+------------+
4 rows in set (0.00 sec)
是的,每個預約有一個簽入和一個結帳,以及它必須包含至少一個房間,或更多(這就是爲什麼ID 6,7,8)。我使用PHP 5.5.13,MySQLi準備語句和Maria DB 10.0.12。 這是PHP代碼片段,將打印輸出表:
<?php
/* This will be the received POST date in the future */
$desdeP = "2014-07-01";
$hastaP = "2014-07-31";
/* Database Connection and DateTime objects */
$conn = new MySQLi("localhost","user","password","database"); //Seriusly? Nope ;)
$desde = DateTime::createFromFormat("Y-m-d",$desdeP);
$hasta = DateTime::createFromFormat("Y-m-d",$hastaP);
?>
<!-- Table with date headers -->
<table border="1" cellpadding="0" cellspacing="0" style="min-width:100%;min-height:100%;">
<tr>
<td> </td>
<?php
while($desde<=$hasta){
echo '<td>'.$desde->format("d-m-Y").'</td>';
$desde->modify("+1Day");
}
?>
</tr>
<?php
/* Query all the rooms */
$stmt = $conn->prepare("SELECT id, name FROM rooms ORDER BY id;");
$stmt->execute();
$rooms = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
/* Query all the bookings in the given dates */
$stmt = $conn->prepare("SELECT idsRoom, checkin, checkout
FROM bookings
WHERE checkout >= ? AND checkin <= ?;");
$stmt->bind_param("ss",
$desdeP,
$hastaP);
$stmt->execute();
$bookings = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
foreach($rooms as $k=>$v){ // Every row is a room
echo '<tr>';
echo '<td>'.$v['name'].'</td>';
$desde = DateTime::createFromFormat("Y-m-d",$desdeP);
$hasta = DateTime::createFromFormat("Y-m-d",$hastaP);
while($desde<=$hasta){ // For every row, check in the bookings list if available or not
echo '<td';
foreach ($bookings as $k2=>$v2){
$checkin = DateTime::createFromFormat("Y-m-d",$v2['checkin']);
$checkout = DateTime::createFromFormat("Y-m-d",$v2['checkout']);
/* HERE remains my question: What mega IF do I need to paint background in red if the room is not available? */
if((strpos(",".$v2['idsRoom'].",",",".$v['id'].",")!==false) &&
($checkin < $hasta && $checkout > $desde) &&
($checkin >= $desde && $checkout <= $hasta)
){
echo ' style="background-color:red;"';
}
}
echo '> </td>';
$desde->modify("+1Day");
}
echo '</tr>';
}
$conn->close();
?>
</table>
感謝所有提前,希望能找到一些幫助吧〜
商祺!
您似乎有一列名爲'idsRoom'的逗號分隔值。在這種情況下,最有效的解決方案包括一罐汽油和一些火柴。 – Strawberry
是的,這是我的解決方案(現在)來存儲預訂和哪些房間與每個預訂相關聯。這是一個很好的解決方案,還是可以改進的? –
這是一個**糟糕的**解決方案。見正常化。 – Strawberry