[英]PHP: Create array from MySQL to build HTML calendar
currently I am working on a small booking system for what I want to create an overview of already booked time slots. 目前,我正在为一个小型预订系统工作,以创建一个已经预订的时间段的概览。
In my database the booking information is saved like this: 预订信息在我的数据库中保存如下:
_______________________________________________________________
|id|user_id| starttime | endtime |facilityID|
---------------------------------------------------------------
|1 | 100|2017-08-21 21:00:00|2017-08-21 22:00:00| 11|
---------------------------------------------------------------
Now I want to build a HTML-table, showing a timetable for the current day, also representing all free timeslots. 现在,我想构建一个HTML表,显示当前一天的时间表,还表示所有空闲时隙。
<table>
<thead>
<th>Time</th>
<th>Facility #11</th>
<th>Facility #12</th>
</thead>
<tr>
<td>08:00</td>
<td>09:00</td>
...
<td>21:00</td>
<td>22:00</td>
</tr>
<tr>
<td></td>
<td></td>
...
<td>User 100</td>
<td></td>
</tr>
</table>
Time Facility #11 Facility #12
08:00 09:00 21:00 22:00
User 100
To populate the table automatically, I thought it would be a good idea to create an associative array containing all possible starttimes as the key and the booking information as the value, if existing. 为了自动填充该表,我认为创建一个包含所有可能的开始时间作为键并将预订信息作为值(如果存在)的关联数组是一个好主意。
array(xy) {["08:00"][1]=>string(0) "" [2]=>string(0) "" ["09:00"][1]=>string(0) "" [2]=>string(0) "" ... ["21:00"] [1]=>string(3) "100" [2]=>string(2) "11" ... }
Therefore I created two arrays which I want to combine to one. 因此,我创建了两个要合并为一个的数组。
Array 1 - Starttimes 数组1-开始时间
array1 = array();
for ($hours = 8; $hours <= 22; $hours++) {
for ($m = 0; $m < 60; $m += 60) {
$starttime_html = sprintf('%02u:%02u', $hours, $m);
$array1[$starttime_html]='';
}
}
Array 2 - Bookings 数组2-预订
array2 = array();
$statement = $pdo->prepare("SELECT id, user_id, starttime, facility_id FROM mpbs_bookings WHERE starttime >= '2017-08-21 00:00:00'");
$statement->execute(array($id));
while($row = $statement->fetch()) {
$starttime_currentday = explode(" ",$row['starttime']);
$starttime_curr_hm = substr($starttime_currentday[1],0,5);
array_push($array2, array($starttime_curr_hm, $row['facility_id'], $row['user_id']));
}
Now I do not find a way to combine those two arrays. 现在,我找不到组合这两个数组的方法。 Besides that, I would be glad about any suggestions on how to achieve my goal.
除此之外,对于实现目标的任何建议我都会感到高兴。
Here are the contents of boths arrays: 这是boths数组的内容:
print_r(array1);
Array ( [08:00] => [09:00] => ... => [21:00] => [22:00]=> )
print_r(array2);
Array ( [0] => Array ( [0] => 18:00 [1] => 6 [2] => 22 ) [1] => Array ( [0] => 19:00 [1] => 4 [2] => 11 ) [2] => Array ( [0] => 20:00 [1] => 2 [2] => 5 ) [3] => Array ( [0] => 20:00 [1] => 9 [2] => 8 ) [4] => Array ( [0] => 21:00 [1] => 11 [2] => 34 ) )
Thanks in advance. 提前致谢。
Lars 拉尔斯
I made a change to the structure of Array2. 我对Array2的结构进行了更改。 I did this so we can make use of array_merge.
我这样做是为了可以利用array_merge。 array_merge works by matching the array keys and replacing the old (array 1) with our new (array 2).
array_merge通过匹配数组键并将旧的(数组1)替换为新的(数组2)来工作。
Please see the ammended code to array2 and a solution to print out the output 请查看array2的扩展代码和打印输出的解决方案
$array2 = array();
$statement = $pdo->prepare("SELECT id, user_id, starttime, facility_id FROM mpbs_bookings WHERE starttime >= '2017-08-21 00:00:00'");
$statement->execute(array($id));
while($row = $statement->fetch()) {
$starttime_currentday = explode(" ",$row['starttime']);
$starttime_curr_hm = substr($starttime_currentday[1],0,5);
$array2[$starttime_curr_hm] =
array('facliId'=>$row['facility_id'],'userid'=> $row['user_id']);
}
$mergedArray = array_merge($array1,$array2);
foreach($mergedArray as $date=>$value){
echo $date;
echo 'Holding facility_id of '.$value['facliId'].' and user_id of '.$value['userid'].'</br>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.