Forum Moderators: coopster

Message Too Old, No Replies

Why won't this simple while end?

         

bobnew32

10:45 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



I'm doing a simple while statement and code to create a page navigation with only simple numbers. It should end, but the damn code goes on forever!

$pages= 5;
$i= 0;

while($i < $pages)
{
$i++;

if($i=$page)
//if integer hits our page we're viewing
{
$navbar .= " $i";
}

else
{
$navbar .= ' <a href="/link.php?fn=view&page=' . $i . '">';
}

}//end while and nav

print $navbar;

marcs

11:18 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



Don't really know enough PHP, but in C/C++, this line might be your problem :

if($i=$page)

In C/C++, you'd need to change that to

if($i==$page)

Otherwise i gets assigned the value of page which may indeed create an endless loop, depending on the value of page.

Timotheos

11:23 pm on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also have the variable $pages and another one that's $page. Is that a typo?

bobnew32

11:26 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



No its not a typo, the variable not present is set elsewhere on the page.

Now can somebody tell me why not having two == was my problem? Thx alot for whom helped, I feel stupid as that was the problem.

Glacai

11:56 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



Hi Bobnew32,
== is a comparison, does this equal this, single = assigns the value so $i was always made to equal $page.

bobnew32

12:02 am on Nov 19, 2003 (gmt 0)

10+ Year Member



:D Thx

coopster

1:01 am on Nov 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just for reference:
Assignment Operators [php.net]
Comparison Operators [php.net]
:)