Jump to content


Photo

broken loop?


  • Please log in to reply
6 replies to this topic

#1 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 23 March 2006 - 01:14 AM

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)
<?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
   }
?>

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
got a problem? Google helps many of those in need

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 March 2006 - 02:40 AM

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:
<?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?
} ?>

Ken

#3 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 23 March 2006 - 03:06 AM

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
<?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
          } 
   }
?>

$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
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                                                                            

got a problem? Google helps many of those in need

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 March 2006 - 03:38 AM

This line:
<?php $page = $id_main/(5*$x); // divide # of rows by 5*$x ?>
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

#5 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 23 March 2006 - 04:24 AM

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
$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;}

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
got a problem? Google helps many of those in need

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 March 2006 - 05:18 AM

This statement in PHP
<?php $page = 7/(5*$x); ?>
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

#7 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 23 March 2006 - 09:22 PM

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
got a problem? Google helps many of those in need




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users