php - Displaying a day of bookings for a room -
i working on school room booking system having trouble outputting bookings.
public function displayroombyday() { $query = db::getinstance()->query("select b.roomid, b.period, r.roomname, b.bookingdate, b.bookingid booking b inner join room r on b.roomid = r.roomid bookingdate = '2016-11-03' , b.roomid=1 order b.period"); //inner join $count = $query->count(); $freecount = 0; for($periods=1;$periods<=6;$periods++) { for($x=0;$x<$count-1;$x++) { $outputted=false; $freecount=0; { if($query->results()[$x]->period == $periods) { echo $query->results()[$x]->period . '<br>'; $outputted=true; } else { echo 'free' . '<br>'; $freecount = 1; } } while($outputted = false , $freecount=0); } } }
this function use output data. sql query returns 2 items, booking in period 5 , booking in period 1 (i have tested through phpmyadmin). trying use nested loops , while loop loop through periods available in day (6). there loop through 2 bookings sql query returns code:
for($x=0;$x<$count-1;$x++) { $outputted=false; $freecount=0; { if($query->results()[$x]->period == $periods) { echo $query->results()[$x]->period . '<br>'; $outputted=true; } else { echo 'free' . '<br>'; $freecount = 1; } } while($outputted = false , $freecount=0); }
however when run page, 1 free free free free free, when trying 1 free free free free 5 when bookings are.
your logic complicated task. can simplify like:
public function displayroombyday() { $query = db::getinstance()->query(" select b.roomid, b.period, r.roomname, b.bookingdate, b.bookingid booking b inner join room r on b.roomid = r.roomid bookingdate = '2016-11-03' , b.roomid=1 order b.period"); //inner join $results = array_reduce($query->results(), function ($carry, $item) { $carry[$item->period] = $item; return $carry; }, []); $periods = range(1, 6); foreach ($periods $period) { if (isset($results[$period]) { echo $period . '<br />'; } else { echo 'free' . '<br />'; } } }
i think that's need.
Comments
Post a Comment