Jump to content

[SOLVED] Trouble with looping through an array


Perad

Recommended Posts

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?

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>";

}

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.