Problem with while loop..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Problem with while loop..

Post 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>";
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Problem with while loop..

Post by sergio-pro »

You should escape quotes in strings

Code: Select all

 
echo "<option value=\"$year-1\">" . ($year-1) . "</option>";
 
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: Problem with while loop..

Post 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?
phoenixrises
Forum Newbie
Posts: 8
Joined: Sun Nov 15, 2009 8:24 am

Re: Problem with while loop..

Post 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.
Ben11858
Forum Newbie
Posts: 16
Joined: Sat Nov 28, 2009 11:27 am

Re: Problem with while loop..

Post by Ben11858 »

Hahaha... OMG, i dunno how i missed that, thanks for that .. Dont you hate it when its something so simple..
Post Reply