Page 1 of 1

"for" loop doesn't break every time, can't find th

Posted: Sun Sep 09, 2007 3:38 am
by arukomp
Hi, here's my for loop:

Code: Select all

<table border=1 cellpadding=5 cellspacing=5 width=100%>
<tr>
	<td width=14% align=center><b>Sunday</b></td>
	<td width=14% align=center><b>Monday</b></td>
	<td width=14$ align=center><b>Tuesday</b></td>
	<td width=14% align=center><b>Wednesday</b></td>
	<td width=14% align=center><b>Thursday</b></td>
	<td width=15% align=center><b>Friday</b></td>
	<td width=15% align=center><b>Saturday</b></td>
</tr>

<tr align=center>
<?php

for (;;) {
	if ($date['wday'] != $tableWeek) {
		for (; $date['wday'] != $tableWeek; ++$tableWeek) {
			echo "<td></td>\n";
		}
		echo "<td bgcolor=\"#7FFFD4\" id=\"{$date['mday']}\" onclick=\"changeCol({$date['mday']})\" align=center style=\"font-size:24px; font-weight:bold;";
		if ($date['wday'] == 0 || $date['wday'] == 6) {
			echo " color:red;";
		}
		echo "\">";
		echo date("j", $timestamp);
		echo "</td>\n";
		$tableWeek++;
		if ($tableWeek > 6) {
			echo "</tr>\n<tr>";
			$tableWeek = 0;
		}
	}
	else {
		echo "<td bgcolor=\"#FFFFFF\" id=\"{$date['mday']}\" onclick=\"changeCol({$date['mday']})\" align=center style=\"font-size:24px; font-weight:bold;";
		if ($date['wday'] == 0 || $date['wday'] == 6) {
			echo " color:red;";
		}
		echo "\">";
		echo date("j", $timestamp);
		echo "</td>\n";
		$tableWeek++;
		if ($tableWeek > 6) {
			echo "</tr>\n<tr>";
			$tableWeek = 0;
		}
	}
	$timestamp += 60 * 60 * 24;
	$date = getdate($timestamp);
	if ($date['mon'] > $month || $date['mon'] < $month) {
		break;    // break loop if another month was reached
	}
}

echo "</tr></table>\n<br><br>";
Before that loop there are 5 values set - $day, $month, $year, $timestamp (timestamp of the first day of the month) and $tableWeek=0 (it indicates current table cell's position, to know what day to put on that cell or to skip that cell.

The problem is, that the browser crashes when specific month is chosen. For example, it displays 2007 year 9 and 11th months perfectly, but on 10th month the browser crashes and I can't fins the problem which causes that. I think the "for" loop somehow becomes infinitive on that month, but I can't find where. When another month is reached, it should break the loop.

Thanks for any help.

Posted: Sun Sep 09, 2007 4:32 am
by s.dot
What the heck is for (;;) ?

Maybe it's valid.. I've just never seen it that way.

EDIT| I guess it is valid. But it does cause infinite loops since it evaluates to TRUE.

Posted: Sun Sep 09, 2007 4:54 am
by arukomp
Yes, it evaluates to TRUE every time, but every cycle this code is executed:

Code: Select all

if ($date['mon'] > $month || $date['mon'] < $month) {
	break;
}
This code should break the loop if another month value is reached. It should reach another value after about 31 loops, because $timestamp is increased by 1 day every cycle, so it should reach another month, which should evaluate TRUE to the above expression and break the loop. But it doesn't break the loop every time. That's the problem.

EDIT: Are talking about for(;; ) part? Well, sometimes it's useful to give no conditions to the loop, and break the loop when you choose to, especially when there are several possibilities to end the loop. I learned that somewhere, but now I don't remember if it was C++ tutorial or PHP :)

Posted: Sun Sep 09, 2007 6:52 am
by superdezign
arukomp wrote:Yes, it evaluates to TRUE every time, but every cycle this code is executed:

Code: Select all

if ($date['mon'] > $month || $date['mon'] < $month) {
	break;
}
This code should break the loop if another month value is reached. It should reach another value after about 31 loops, because $timestamp is increased by 1 day every cycle, so it should reach another month, which should evaluate TRUE to the above expression and break the loop. But it doesn't break the loop every time. That's the problem.

EDIT: Are talking about for(;; ) part? Well, sometimes it's useful to give no conditions to the loop, and break the loop when you choose to, especially when there are several possibilities to end the loop. I learned that somewhere, but now I don't remember if it was C++ tutorial or PHP :)

Code: Select all

do {
    // etc.
} while ($date['mon'] != $month);

Posted: Sun Sep 09, 2007 7:35 am
by arukomp
So... I've changed "for" loop into "do... while" loop and it worked great! :D Still I can't understand what was so different and why didn't my old loop work... But anyway, it worked, that's most important.

Thanks again :)

Posted: Sun Sep 09, 2007 8:17 am
by superdezign
You should always avoid infinite loops, especially so in web programming, as you will almost never need them.