PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I made a couple of month ago a PHP page which implements pagination: then as I do now I put the problems encountered on the forum. At that I didn' t needed to put a restriction on the links to the different pages resulted from the pagination but now I see I have to.
I' m trying to display something like
if you are on page 3: 12345 6
if you are on page 5: 345 678
Here is the part of the code that outputs the links
/*
$Current indicates the current page
$i. $j, $l the indicators for the positions
$totalN the total number of pages
*/
$j=$i+3;
$l = $Current- 2;
do
{
if($i!=$Current && $i<$j && $l<0)
{echo "<a href='categ.php?cat=" .$_GET['cat'] ."&page=" .$i ."'>" .$i."</a>";}
// displaying the first two links i.e. 3 , 4
else if ($l>0)
{
for ($t=$l; $t<$Current; $t++)
{echo "<a href='categ.php?cat=" .$_GET['cat'] ."&page=" .$l ." '>" .$i."</a>";}
}
else if ($i==$Current){echo "<b>" .$i ."</b>";}
$i=$i+1;
}
while ($i<=$totalN || $i< $j);
The following code shows from the current page to the last page.
Any ideas where I screw up or any ideas for a nicer display (links ) ?
Last edited by Rovas on Thu Feb 22, 2007 12:32 pm, edited 2 times in total.
Get a piece of paper and write out exactly what is happening, statement-by-statement, for several values with the code you've posted. .. or alter the echoes such that they differ so you can see which one is being used.
I solved it (I' am not totally satisfied with the solution but it works). Thanks feyd for help (too tired yesterday to understand what you meant). The ideea is simple think of number intervals from mathematics and from there is a piece of cake:
$a= $Current-2;
if ($a<=0) {$a=1;}
switch ($totalN- $Current)
case 0: $b=1; break;
case 1: $b=2; break;
case 2: $b=2; break;
for($i=$a; $i<=$b: $i++)
{
// similar to the former do.. while
}
Thank you but I have made it' s enough: I made some modifications since the last post and it' working
very good (it 's similar the forum when there a lot of pages > 20). Anybody can take our code and modify to suit their needs.
[EDIT]Of course the code written onion2k is better.[/EDIT]