PHP: Create array from MySQL to build HTML calendar -
currently working on small booking system want create overview of booked time slots.
in database booking information saved this:
_______________________________________________________________ |id|user_id| starttime | endtime |facilityid| --------------------------------------------------------------- |1 | 100|2017-08-21 21:00:00|2017-08-21 22:00:00| 11| --------------------------------------------------------------- now want build html-table, showing timetable current day, representing 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 table automatically, thought idea create associative array containing possible starttimes key , booking information 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 created 2 arrays want combine one.
array 1 - starttimes
array1 = array(); ($hours = 8; $hours <= 22; $hours++) { ($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 mpbs_bookings 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 not find way combine 2 arrays. besides that, glad suggestions on how achieve goal.
here 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 change structure of array2. did can make use of array_merge. array_merge works matching array keys , replacing old (array 1) our new (array 2).
please see ammended code array2 , solution print out output
$array2 = array(); $statement = $pdo->prepare("select id, user_id, starttime, facility_id mpbs_bookings 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 $date=>$value){ echo $date; echo 'holding facility_id of '.$value['facliid'].' , user_id of '.$value['userid'].'</br>'; }
Comments
Post a Comment