Forum Moderators: coopster

Message Too Old, No Replies

Pagination

Nearly there

         

ukgimp

2:08 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello

I have been trying to get my pagination to work as it should. I am nearly there.

For example if I have 10 pages and the increment between them is set to one, all is well unitl I get to page 9. It should show

Prev 1 2 3 4 5 6 7 8 9 10 Next

but it misses the Next off. Any chance someone could pass their capable eyes of my code to see where I may be going wrong. It is not life threatening, just hindering me somewhat.

Cheers

##################
$filename = "test";
$page_num =9;
$s= $page_num;

//increment between pagination eg 1 2 3 next == 1
$n = 1;

$num_pages = 10;
$total_pages = $num_pages;
$e = min($s + $n - 1, $s + $num_pages - 1);

$pagin = "";
$pagin .= "<div id=\"options\">\r\n";
if ($s!= 1){
$previous = $s - $n;
if($previous == 1) {
$pagin .= "<A HREF=\"$filename.php\">Previous</a>\r\n";
}
else {
$pagin .= "<A HREF=\"$filename$previous.php\">Previous</a>\r\n";
}
};
for ($x = 1; $x <= ceil($num_pages/$n); $x++){
$current = ($x-1) * $n + 1;
if ($current == $s){
$pagin .= "$x\r\n";

}else{
if($current == 1)
{
$pagin .= "<A HREF=\"$filename.php\">$x</a>\r\n";
}
else{
$pagin .= "<A HREF=\"$filename$current.php\">$x</a>\r\n";
}
};
};
if ($e + $s -1 < $total_pages){
$next = $s + $n;
$pagin .= "<A HREF=\"$filename$next.php\">Next</a>\r\n";
};
$pagin .= "</div>";
print "$pagin";

Birdman

2:31 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick guess would be that this math part is not calculating in the proper order:

if ($e + $s -1 < $total_pages){

On something like that you need to break the math down into brackets. Well, I can't fully explain it but this page does:

PHP Operators [php.net]

Scroll down a bit, to the 'Operator Precedence' section.

Cheers,
Birdman

ukgimp

3:49 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers Birdman

Found a solution.

That line you pointed out should be

if ($page_num < $total_pages){

Cheers

coopster

3:59 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It isn't the precedence, but the value the formula is producing.
... 
print "$pagin";
print "<hr>e + s minus 1 is < total_pages?<br>";
print "<b>$e + $s - 1 < $total_pages</b><hr>";
// returns:
// e + s minus 1 is < total_pages?
// (9 + 9 - 1) < 10

Anyway, without digging into your logic too deeply, your code seems to work if you change the comparison to something like this:
if ($e!= $total_pages){ 

Too slow ;-)

coopster

4:06 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was playing around a bit with a
for
loop, too. Have you considered a
for
loop as opposed to all the arithmetic, functions and comparisons you currently have? It's much quicker, consistently 50% faster. Test code coming here, but if you want to play around with it, it's there. Regards, coopster.

$pagin = "<div id=\"options\">\r\n";
$start = 1;
$show = 10;
$current = 9;
$increment = 1;
if ($current <> 1) $pagin .= "<a href=\"$filename" . ($current - 1) . ".php\">Previous</a>\r\n";
for ($i = $start; $i <= $show; $i += $increment) {
if ($i == $current) {
$pagin .= "$i\r\n";
} else {
$pagin .= "<a href=\"$filename$i.php\">$i</a>\r\n";
}
}
$pagin .= "<a href=\"$filename" . ($current + 1) . ".php\">Next</a>\r\n";
$pagin .= "</div>";
print $pagin;