【发布时间】:2018-04-18 13:28:30
【问题描述】:
我必须为房间预订系统创建一个 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'
]
];
如果我遗漏了任何内容或对我的描述过于含糊,我深表歉意。谢谢你
【问题讨论】:
-
如果屏幕截图与示例数据匹配会更清晰
-
这个数据结构:
1 => [ 0 => 'Room 1', 1 => [ 'room_Name' => 'Room 1', 'start_Time' => '09:30', 'end_Time' => '11:30' ], 2 => [ 'room_Name' => 'Room 1', 'start_Time' => '13:00', 'end_Time' => '14:30' ] ];似乎有点不靠谱。您是否只需要假设元素 0 之后的所有内容都代表预订? -
1 => [ "Room" => 'Room 1', "Bookings" => [ 0 => [ 'room_Name' => 'Room 1', 'start_Time' => '09:30', 'end_Time' => '11:30' ], 1 => [ 'room_Name' => 'Room 1', 'start_Time' => '13:00', 'end_Time' => '14:30' ] ] ];这样的东西会更合乎逻辑。并不是说它可以解决您的问题,但可能会使数据更易于处理。 -
同样在最后一段代码中(我假设它实际上属于 before 现实中的第一位?)您定义了 $roomBooking,但在其他代码中您使用了 $newBookings .它们实际上是同一件事吗?这有点不清楚。您是否将两个版本的代码合并在一起,或者在创建最小示例时犯了错误?
-
是的,我只是在发布问题后才注意到它不匹配,所以对此我深表歉意。我在他们那里设置数组的方式是我需要 0 元素来获取标题的房间名称。但我可以将它分成自己的数组并循环遍历。但是,是的,它有点笨拙,我将对其进行更改,以便数据更易于处理。谢谢!