简体   繁体   中英

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.

<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

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

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:

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. I did this so we can make use of array_merge. array_merge works by matching the array keys and replacing the old (array 1) with our new (array 2).

Please see the ammended code to array2 and a solution to print out the output

$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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM