[英]HTML table creation in php for a room booking system
我必須為房間預訂系統創建一個 html 表,但我在想辦法將數據插入單元格時遇到了麻煩。 目前我正在從數據庫中獲取數據(預訂的開始時間和結束時間)。 然后我循環遍歷數組中的時間並使用 if 語句將開始時間與時間數組中的時間進行比較。
這就是我目前所擁有的:房間預訂圖片
我遇到的主要問題是,在我的數據庫中,如果同一天有兩個預訂,它只會顯示數組中的最后一個。
$tableStart = "<table class='table_main'><thead><tr id='table_first' class='first_last'><th class='first_last'>Time:</th>";
$tableMid = "</tr></thead><tbody>";
$tableEnd = "</tbody></table>";
foreach ($newBookings as $booking) {
$tableStart .= "<th class='first_last'>$booking[0]</th>";
}
foreach ($times as $time) {
$tableMid .= "<tr class='even_row'><td class='time'>{$time['times']}</td>";
$i = 0;
foreach ($newBookings as $booking) {
unset($booking[0]);
$x = count($booking);
if ($x > 0) {
if ($booking[$i]['start_Time'] == $time['times']) {
$tableMid .= "<td class='new'>{$booking[$i]['start_Time']}</td>";
} else if($booking[$i]['end_Time'] == $time['times']) {
$tableMid .= "<td class='new'>{$booking[$i]['end_Time']}</td>";
} else {
$tableMid .= "<td class='new'></td>";
}
} else {
$tableMid .= "<td class='new'></td>";
}
++$i;
}
}
$tableStart .= $tableMid;
$tableStart .= $tableEnd;
return $tableStart;`
我目前在數組中設置數據的方式目前如下所示:
$times = [
0 => ['06:00'],
1 => ['06:30'],
2 => ['07:00'],
3 => ['07:30'],
4 => ['08:00'],
5 => ['08:30'],
6 => ['09:00'],
7 => ['09:30'],
8 => ['10:00'],
9 => ['10:30'],
10 => ['11:00'],
11=> ['11:30'],
12 => ['12:00'],
13 => ['12:30'],
14 => ['13:00'],
15 => ['13:30'],
16 => ['14:00'],
17 => ['14:30'],
18 => ['15:00'],
19 => ['15:30'],
20 => ['16:00'],
21 => ['16:30'],
22 => ['17:00'],
23 => ['17:30'],
24 => ['18:00'],];
$newBookings = [
0 => [
0 => 'Room 33'
],
1 => [
0 => 'New room 8',
1 => [
'room_Name' => 'New room 8',
'start_Time' => '11:30',
'end_Time' => '12:30'
]
],
2 => [
0 => 'sds',
1 => [
'room_Name' => 'sds',
'start_Time' => '09:30',
'end_Time' => '11:30'
],
2 => [
'room_Name' => 'sds',
'start_Time' => '14:30',
'end_Time' => '16:30'
]
],
3 => [
0 => 'New Room 3'
],
4 => [
0 => 'NewRoom5'
],
5 => [
0 => 'New room 55'
]
];
如果我遺漏了任何內容或我的描述過於含糊,我深表歉意。 謝謝
對於看到這一點的任何人,我找到了一個修復程序,我使用了 ADyson 對陣列的建議並重新編寫了我的代碼,以便它現在可以正常工作。 對於任何對此感興趣的人,我使用的代碼是:
public function render(): string
{
$data = $this->tableData; //from abstract class contains table data
$times = $this->tableTimes; //gets times
$rooms = $this->roomNames; //gets room names
$startTime = $times[0]['start_time']; //sets the start time
$endTime = $times[0]['end_time']; // sets the end time
$times = $this->setTimeArray($startTime, $endTime); //goes to function
$newBookings = $this->setDataArray($data, $rooms); // goes to function
$tableStart = "<table class='table_main'><thead><tr id='table_first' class='first_last'><th class='first_last'>Room:</th>";
$tableMid = "</tr></thead><tbody>";
$tableEnd = "</tbody></table>";
foreach ($times as $key => $time) {
$tableStart .= "<th class='first_last'>{$time}</th>";
}
foreach ($newBookings as $booking) {
$tableMid .= "<tr class='even_row'><td class='room_Name'>{$booking['Room']}</td>";
$x = 0;
$e = 0;
foreach ($times as $time) {
if ($x == count($booking['Bookings'])) {
$x = 0;
}
if ($e == count($booking['Bookings'])) {
$e = 0;
}
if (count($booking['Bookings']) < 1) {
$tableMid .= "<td class='new'></td>";
} else if ($booking['Bookings'][$x]['start_Time'] == $time) {
$tableMid .= "<td class='room_Booked'>{$booking['Bookings'][$x]['start_Time']}</td>";
$x++;
} else if ($booking['Bookings'][$e]['end_Time'] == $time) {
$tableMid .= "<td class='room_Booked'>{$booking['Bookings'][$e]['end_Time']}</td>";
$e++;
} else {
$tableMid .= "<td class='new'></td>";
}
}
}
$tableStart .= $tableMid;
$tableStart .= $tableEnd;
return $tableStart;
}
將時間設置為數組
public function setTimeArray($startTime, $endTime)
{
$i = 0;
$endTimes = strtotime($endTime);
$endTime = date("H:i", strtotime('+30 minutes', $endTimes));
do {
$times[$i] = $startTime;
$time = strtotime($startTime);
$startTime = date("H:i", strtotime('+30 minutes', $time));
$i++;
} while ($startTime != $endTime);
return ($times);
}
將房間預訂數據放入一個數組
public function setDataArray($data, $rooms)
{
$newBookings = [];
$x = 0;
foreach ($rooms as $key) {
$newBookings[$x] = array(
'Room' => $key['room_name'], 'Bookings' => []
);
$y = 0;
foreach ($data as $value) {
if ($value['room_name'] == $newBookings[$x]['Room']) {
$newBookings[$x]['Bookings'][$y] = [
'room_Name' => $value['room_name'],
'start_Time' => $value['requested_time'],
'end_Time' => $value['requested_time_end']
];
$y++;
}
}
$x++;
}
return ($newBookings);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.