Forum Moderators: coopster
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";
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
...
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
if ($e!= $total_pages){
Too slow ;-)
forloop, too. Have you considered a
forloop 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;