Page 1 of 1

Problem with while loop..

Posted: Sat Nov 28, 2009 11:44 am
by Ben11858
I'm setting up a while loop to run through years up to a maximum of 80 for a select form box so I don't manually write the code in and waste a lot of time and lots of code to my page. Although its taking me longer now I have this error, but I'm hoping someone can help explain it.. This is my simple code...

Code: Select all

 
$counter = 0;
$year = 2000;
while($counter <= 80){
    echo "<option value ="$year-1">"$year-1"</option>";
     }

And when i run the page i receive this error instead...

Code: Select all

 
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\xampp\htdocs\Briefsystem\Accused.php on line 77
Doesn't make much sense seeing as I do have a ; there.. Any help please.. And line 77 is

Code: Select all

echo "<option value ="$year-1">"$year-1"</option>";

Re: Problem with while loop..

Posted: Sat Nov 28, 2009 11:50 am
by sergio-pro
You should escape quotes in strings

Code: Select all

 
echo "<option value=\"$year-1\">" . ($year-1) . "</option>";
 

Re: Problem with while loop..

Posted: Sun Nov 29, 2009 12:56 am
by Ben11858
Cheers champ... That seemed to help that error message. But now instead of an error msg I get a different problem.

The page loads up until the point of where it reaches the loop, then it just seems to chew up heaps of CPU power and memory processing the loop, in the end it basically stops responding and I have to kill IE through task manager to get my computer working properly again.. I've left it for about 10mins and still no joy.. Any ideas?

Re: Problem with while loop..

Posted: Sun Nov 29, 2009 3:47 am
by phoenixrises
You don't seem to change the value of $counter so you're getting an infinite loop by the looks of it. To fix, increment $counter within the while loop.

Re: Problem with while loop..

Posted: Sun Nov 29, 2009 4:04 am
by Ben11858
Hahaha... OMG, i dunno how i missed that, thanks for that .. Dont you hate it when its something so simple..