用php为客房预订系统创建HTML表
我必须为一个房间预订系统创建一个html表,但我在尝试将数据插入单元格时遇到了麻烦。目前,我正在从数据库中获取数据(预订的开始时间和结束时间)。然后,我从一个数组中循环时间,并使用if语句将开始时间与时间数组中的时间进行比较 这就是我目前的情况: 客房预订图像 我遇到的主要问题是,在我的数据库中,如果在同一天有两个预订,它将只显示数组中的最后一个预订用php为客房预订系统创建HTML表,php,html,laravel,Php,Html,Laravel,我必须为一个房间预订系统创建一个html表,但我在尝试将数据插入单元格时遇到了麻烦。目前,我正在从数据库中获取数据(预订的开始时间和结束时间)。然后,我从一个数组中循环时间,并使用if语句将开始时间与时间数组中的时间进行比较 这就是我目前的情况: 客房预订图像 我遇到的主要问题是,在我的数据库中,如果在同一天有两个预订,它将只显示数组中的最后一个预订 $tableStart = "<table class='table_main'><thead><tr i
$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;`
如果我遗漏了任何内容或描述过于模糊,我深表歉意。谢谢你对于任何看到这一点的人,我找到了一个修复方法,我使用了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 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);
}
如果屏幕截图与示例数据相匹配,则会更加清晰。此外,此数据结构:
1=>[0=>'Room 1',1=>['Room\u Name'=>'Room 1','start\u Time'=>'09:30','end\u Time'=>'11:30',2=>['room\u Name'=>'room 1'、'start\u Time'=>'13:00'、'end\u Time'=>'14:30']];
似乎有点不太靠谱。您是否必须假设元素0之后的所有内容都代表预订?类似于1=>[“room”=>'room 1'、“Bookings”=>[0=>[“房间名称”=>“房间1”,“开始时间”=>“09:30”,“结束时间”=>“11:30”],1=>[“房间名称”=>“房间1”,“开始时间”=>“13:00”,“结束时间”=>“14:30”]]
会更符合逻辑。不要说它解决了您的问题,但可能会使数据更易于处理。还有在代码的最后一位(我假设它实际上属于第一位之前?)你定义了$roomBooking,但在另一个代码中你使用了$newBookings。它们实际上是相同的还是不同的?这有点不清楚。你是否将两个版本的代码合并在一起,或者在创建一个最小的示例或其他东西时犯了错误?是的,我只是在发布问题后才注意到它不匹配,所以我为此道歉。还有我现在设置数组的方法是,我需要0元素来获取标题的房间名称。但我可以将其分离到自己的数组中并循环使用。但是,是的,它有点粗糙,我会更改它,以便数据更易于处理。谢谢!
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);
}