Perad Posted August 17, 2007 Share Posted August 17, 2007 I am having some trouble with a booking system. What I want to do it is make booked times display in red. Available times in white. Below is my function. It works for no, or one booking. However if you do a second booking everything comes unstuck, its pretty obvious why but I can't figure out any other way to do this. public function DisplayDay($date) { $theday = date('l', $date); if ($theday == "Saturday") { $starttime = "10"; $endtime = "12"; } else { $starttime = "10"; $endtime = "16"; } $sql = "SELECT * FROM bookings WHERE bookingdate='$date'"; $result = mysql_query($sql); if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $bookedtimes[] = $row['bookingtime']; } } print "<table><tr><td>$theday</td></tr>"; for ($time=$starttime;$time<=$endtime;$time++) { $newtime = $time.":00 - ". ($time+1).":59"; if ($bookedtimes) { foreach($bookedtimes as $value) { echo $time . " = " . $value . "<br />"; if ($time == $value) { echo "<tr><td style=\"background:#713b3b;\">$newtime</td></tr>"; } else { echo "<tr><td style=\"background:#FFF;\"><a href=\"$page?book=$time&date=$date\">$newtime</a></td></tr>"; } } } else { echo "<tr><td style=\"background:#FFF;\"><a href=\"$page?book=$time&date=$date\">$newtime</a></td></tr>"; } $time++; } print "</table>"; } If 1 bookings is made then the array loops through once as so.. 10 = 10 - red 12 = 10 - white 14 = 10 - white 16 = 10 - white If there are 2 bookings then everything goes wrong. 10 = 10 - red 10 = 14 - white 12 = 10 - white 12 = 14 - white 14 = 10 - white 14 = 14 - red 16 = 10 - white 16 = 14 - white Does anyone have any ideas on to how I can rewrite the for statement? Quote Link to comment https://forums.phpfreaks.com/topic/65413-solved-trouble-with-looping-through-an-array/ Share on other sites More sharing options...
Psycho Posted August 17, 2007 Share Posted August 17, 2007 Try this: <?php public function DisplayDay($date) { $theday = date('l', $date); $starttime = 10; $endtime = ($theday == "Saturday") ? 12 : 16; $sql = "SELECT * FROM bookings WHERE bookingdate='$date'"; $result = mysql_query($sql) or Die (mysql_error()); echo "<table><tr><td>$theday</td></tr>"; if ($result) { $bookedtimes = array(); while ($row = mysql_fetch_assoc($result)) { $bookedtimes[] = $row['bookingtime']; } for ($time=$starttime;$time<=$endtime;$time=$time+2) { $newtime = $time . ":00 - " . ($time+1) . ":59"; echo $time . " = " . $value . "<br />"; if (in_array($time,$bookedtimes)) { echo "<tr><td style=\"background:#713b3b;\">$newtime</td></tr>"; } else { echo "<tr><td style=\"background:#FFF;\"><a href=\"$page?book=$time&date=$date\">$newtime</a></td></tr>"; } } } else { //Database error echo "<tr><td style=\"background:#FFF;\">Error retrieving database records</td></tr>"; } print "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65413-solved-trouble-with-looping-through-an-array/#findComment-326718 Share on other sites More sharing options...
Perad Posted August 17, 2007 Author Share Posted August 17, 2007 Ah works a treat, thanks for your time and thank you very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/65413-solved-trouble-with-looping-through-an-array/#findComment-326727 Share on other sites More sharing options...
Psycho Posted August 17, 2007 Share Posted August 17, 2007 Oh, there is one problem with the code I provided. I used an "or DIE" on the database query, but then later I had some code to display an error message if $result was false. If you want to display the friendly error message you would need to remove the "or DIE" clause. Quote Link to comment https://forums.phpfreaks.com/topic/65413-solved-trouble-with-looping-through-an-array/#findComment-326739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.