Jump to content

broken loop?


earl_dc10

Recommended Posts

hey, I have a loop to make the links for the different pages for my site, I want them in groups of 5 (except for the last one if it doesn't divide equally)
[code]
<?php
$page = $id_main/(5*$x); // divide # of rows by 5*$x
   for($x = 1; $page >= 0.2; $x++) // so it still runs if there's only 1 page left
   {echo "<a href=\"".$self."?pid=".$x"\" class=\"page\">".$x." </a>"; // makes all the pages except the final
     if(($page < 1) && ($page > 0))
     {echo "<a href=\"".$self."?pid=".$x."\" class=\"page\">".$x."</a>";} // make last page
   }
?>
[/code]

Nothing displays from the loop, even if I just put in echo $x; inside the loop, it does nothing (and I do have more than 5 pages, so it's not that). I get no errors, so if you see something, please tell me

Thanks
-J
Link to comment
Share on other sites

What is $x and where is it initialized?

If your for loop worked it would never end, since the variable in the end condition ($page) is never modified.

What do you mean by groups of 5? Do you mean:

link1 link2 link3 link4 link5
link6 link7 link8 link9 link10

or

link1
link2
link3
link4
link5

link6
link7
link8
link9
link10

Here's some code that will present your links as in the first set:
[code]<?php
$number_rows = 22;  
for ($i=1; $i <= $number_rows; $i++) {
     echo '[<a href="'.$_SERVER['PHP_SELF'] . '?pid=' . $i . '" class="page">Link ' . $i . '</a>] ';
     if ($i % 5 == 0) echo '<br>'; // 5 links output?
} ?>[/code]

Ken
Link to comment
Share on other sites

I apologize, when copying the code (my editor doesn't like to paste to mozilla), I left out the break statement at the end of the if(), my fault, anyways, here's what it actually is
[code]
<?php
$page = $id_main/(5*$x); // divide # of rows by 5*$x
   for($x = 1; $page >= 0.2; $x++) // so it still runs if there's only 1 page left
   {echo "<a href=\"".$self."?pid=".$x"\" class=\"page\">".$x." </a>"; // makes all the pages except the final
     if(($page < 1) && ($page > 0))
     {echo "<a href=\"".$self."?pid=".$x."\" class=\"page\">".$x."</a>"; // make last page
        break; // end script
          }
   }
?>
[/code]

$x is stated in the loop, $page is just a shorter name than $id_main/(5*$x) and so there should be no problem there. and as for it never ending, it would end after $page became less than 1, $x is being modified which in turn changes $page (example below) and how this should output is links to pages of 5 entries each (1 link = 5 entries), should look like this: 1 2 3 4.... all being links, with 1 being the most recent 5 entries and the last having the odd number that are last.

example
[code]
for($x = 1; $page >= 0.2; $x++)

// expected results if $num_main = 7
// $num_main = 7, $x = 1; => 7/(5*1) = 1.4 which is >= 0.2, script runs
// $num_main = 7, $x = 2; => 7/(5*2) = .7 which is >= 0.2, script runs, but runs the if statement
// since it is less than 1, signifying there are less than 5 entries left for the final page and script ends with break                                                                            
[/code]
Link to comment
Share on other sites

This line:
[code]<?php $page = $id_main/(5*$x); // divide # of rows by 5*$x ?>[/code]
is outside the loop, therefore $x is not defined and you should get a divide by zero error.

Use the code I posted. It works and it's much easier to understand.

Ken
Link to comment
Share on other sites

I thought about using the modulus, but wasn't sure how to account for the odd number that would go in the last page (the second 2 in your 22 would be lost) but Im pretty sure I got it, I'll try it in the morning. thanks for your patience!

and, just so I don't feel entirely stupid, put this in a script and run it
[code]
$page = 7/(5*$x);
echo $page; // this gives an error
  if($x = 1)
   {echo $page;} // this gives result

// you were correct about the loop however, Im confused why but oh well

// this yields nothing
$page = 7/(5*$x);
   for($x = 1; $page == 1.4; $x++)
     {echo $page;}
[/code]

because it is not called outside the loop, it doesn't get a divide by 0 error, it is simply an alias for a clumsy equation, which, when you insert $page into the loop, the server reads it as 22/(5*$x) because they are one and the same
Link to comment
Share on other sites

This statement in PHP
[code]<?php $page = 7/(5*$x); ?>[/code]
is an equation. It gets evaluated when the line of code is seen and will get a divide by zero warning if $x is undefined. I did try it. I don't know where you got the idea that it is an "alias". AFAIK, PHP does not have "alaises" like that.

If you don't want to try my code to display your links that's fine, but it works. You can see for yourself at [a href=\"http://www.rbnsn.com/phpfreaks/test_link_display.php\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/test_link_display.php[/a]

Invoking it as above, it will default to 22 links

If you invoke it via [a href=\"http://www.rbnsn.com/phpfreaks/test_link_display.php&x=42\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/test_link_display.php&x=42[/a] you will get a display of 42 links. You can change the number of x to anything. If it is not numeric or zero or less it will default to 22.

Ken
Link to comment
Share on other sites

I did incorporate elements of your code into mine, and it works great now, thank you.

and I humbly accept your correction, the above script apparently doesn't work on its own, but somehow it works incorporated in my site (seriously), but however, it will be the last time I attempt something like that, so thank you
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.