2

I'm making a calendar system where the user makes bookings (on a different page). On this page, the user clicks the relevant box to choose a start time for the new booking. I'm checking the database for existing bookings and making those unclickable to prevent double booking.

My question is, why is only the first booking for that day appearing on the table?

Is the variable $booked remaining true, and so not showing the other bookings? I can't get my head around it!

MySQL:

$query="SELECT * FROM bookings WHERE DateBooked = '{$year}-{$month}-{$selectedday}' AND Approved = 1";
$result = mysql_query($query);
$todayarray = mysql_fetch_assoc($result);

And PHP:

while ($room <= $roomcount) {
    echo "\n<div class=\"roomtimes\">";
    echo "\n<table border=1>";
    echo "\n<tr><th class=\"titlecell\">Room $room</th></tr>";
    $cellnum = 10;
    while ($cellnum <= 22) {
        if (($todayarray['StartTime'] <= $cellnum) && ($todayarray['EndTime'] >= $cellnum) && ($todayarray['Room'] == $room)) {
            $booked = true;
        } else {
            $booked = false;
        }
        echo "\n<tr>";
        if ($booked) {
            echo "\n<td class=\"blankcell";
        } else {
            echo "\n<td class=\"linkcell";
        }

        if ($selectedtime == $cellnum) {
            echo " selectedcell";
        }
        echo "\">";
        if ($booked) {
            echo "$cellnum:00 --BOOKED--";
        } else {
            echo "<a href=\"newbooking.php?m=$selectedmonth&d=$selectedday&t=$cellnum&r=$room\">$cellnum:00</a>";
        }
        echo "</td>";
        echo "\n</tr>";
        $cellnum++;
    }
    $room++;
    echo "\n</table>";
    echo "\n</div>";
}
echo "\n</div>";
?>
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Dan Hughes
  • 27
  • 8

1 Answers1

0

The issues is that you are only looking at the first row of the result set from your table. You only call mysql_fetch_assoc($result) once, this function only returns one result row at a time. You need to iterate through the $result you get from your query call. You can use a while loop similar to this:

while ($row = mysql_fetch_assoc($result) {
   //this can be your php code from above 
} 
Scott
  • 12,077
  • 4
  • 27
  • 48