0
<?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.

2
  • 8
    Replace $i = 1 with $i == 1 in while-loop. Commented Apr 22, 2013 at 11:02
  • Your if-statement is quite complicated. I would recommend replacing it with something like: if(CheckSomeCondition($i, $page, $totalPages)){ ... }, then do the actual logic of the if-check in a more readable fashion in that method (that would probably make it easier for you to find the problem yourself, too). Commented Apr 22, 2013 at 11:24

5 Answers 5

2

Your if condition has a problem

Change

$i =1 

with

$i==1
Sign up to request clarification or add additional context in comments.

Comments

1

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

$i=1

and

$i=$page

replace them with ==

1 Comment

Well, it resets the variable and returns true as the value has been set succesfully
0
<?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

2 Comments

Yup, that's fixed it, thanks for pointit out my own stupidity. :)
Most welcome, always take a time while deciding a conditions it will save your time and make your software bug free.
0

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
}

2 Comments

He has multiple conditions
TRUE but he could still do this with a for loop, all that's happening is he is looping through a list of page numbers and making the active page number bold
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.