Pete Spalding
Pete Spalding

Reputation: 1

PHP Infinite While Loop, Unknown Cause

<?php
$i=1;
while ($i<=$totalpages) {
    if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {

        echo "<td><a href=\"search.php?page=$i\">";
        if ($i=$page) {
        echo "<strong>  $i  </strong>";
        }
        if ($i!=$page) {
        echo "  $i  ";
        }
        echo "</a></td>";
        }
    $i++;
    }

?>

Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.

Upvotes: 0

Views: 92

Answers (5)

Agus
Agus

Reputation: 1614

There is some issues in the conditions

first: the

|| $i=1||

allways return true. Change for

|| $i==1||

Second: The same case of adobe

if ($i=$page) //Allways return try is assignation operation

Change to:

if ($i==$page)

Upvotes: 0

MadDokMike
MadDokMike

Reputation: 182

is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.

eg.

for ($i = 0; $i<=$totalpages; $i++){
 // do something
}

Upvotes: 0

Navnath Godse
Navnath Godse

Reputation: 2225

<?php
        $i=1;
        while ($i<=$totalpages) {
            if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {

                echo "<td><a href=\"search.php?page=$i\">";
                if ($i == $page) {
                echo "<strong>  $i  </strong>";
                }
                if ($i!=$page) {
                echo "  $i  ";
                }
                echo "</a></td>";
                }
            $i++;
            }
?>

Changes in 1st and second if condition.
1) $i == 1
2) $i == $page

Upvotes: 0

Stuart Siegler
Stuart Siegler

Reputation: 1726

You have a number of places that you reset $i, inadvertently I assume

$i=1

and

$i=$page

replace them with ==

Upvotes: 1

chandresh_cool
chandresh_cool

Reputation: 11830

Your if condition has a problem

Change

$i =1 

with

$i==1

Upvotes: 2

Related Questions