I am basing my algorithm on the tutorial at http://www.phpfreaks.com/tutorials/43/4.php
Here is my class and class method to handle the calculation of the "Previous" and "Next" links. Everything works, except the "Next" link displays completely wrong, and for the past 2 hours I have been wracking my brain trying to figure this out, to no avail:
class PaginationView extends View {
var $result; // YOUR RESULTSET
function PaginationView($result) { // CONSTRUCTOR
$this->result = $result;
}
function &displayPage() { // STATIC HTML STRING METHOD
global $section, $action, $album, $headerMenuArray, $willPaginate, $displayItemLimit;
foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} = $val;
$qs = "section=$section&action=$action&sort=$sort&chooseAlbum=1&album=" . urlencode($album) . '&willKeepPageSession=1'; // FOR EASE OF WRITE
if ((int)$page === 0) $page = 1;
$page = (int)$page; // CONVERT TO INTEGER
if (@sizeof($this->result) > $displayItemLimit && $willPaginate) {
$html .= "<div align=\"center\">\n";
// PREVIOUS LINK
if ((int)$page !== 1) {
$pagePrev = $page - 1;
$html .= " <a href=\"index.php?$qs&page=$pagePrev\">Previous $displayItemLimit ${section}s in \"$album\"</a> <b>|</b> ";
}
$numPages = (int)(@sizeof($this->result) / $displayItemLimit);
// ALL PAGES (PAGE NUMBER) LINK(S) EXCEPT FOR LAST PAGE
for ($i = 1; $i <= $numPages; $i++) {
if ((int)$i === (int)$page) $html .= "$i <b>|</b> "; else $html .= "<a href=\"index.php?$qs&page=$i\">$i</a> <b>|</b> ";
}
// LAST PAGE NUMBER LINK
if (@sizeof($this->result) % $displayItemLimit != 0) {
if ((int)$i === (int)$page) $html .= "$i "; else $html .= "<a href=\"index.php?$qs&page=$i\">$i</a> ";
}
// NEXT LINK
$offset = (int)(@sizeof($this->result) - ($displayItemLimit * ($page - 1)));
if ($offset > 0) {
$pageNext = $page + 1;
$html .= " <b>|</b> <a href=\"index.php?$qs&page=$pageNext\">Next ";
$html .= ($offset + (int)$displayItemLimit > @sizeof($this->result)) ? (int)($displayItemLimit - $offset) : $displayItemLimit;
$html .= " ${section}s in \"$album\"</a>";
}
$html .= "\n</div>\n";
}
return $html;
}
}
I have tried everything under the sun and I just can't figure this out logically to save my life. Here is a sample output that results with a $displayItemLimit of 20 items and I'm on page "1" and there are 45 items altogether:
1 | 2 | 3 | Next -25 images in "Album 1"
The problem is the $offset variable I know in the // NEXT LINK code block, but I'm stuck, I can't figure it out, furthermore, both of these utterly fail in PHP 4.3.2 on my system:
$pagePrev = $page--;
$pageNext = $page++;
Both result in $pagePrev and $pageNext having null values even though $page exists and is cast into an integer!
The pagination functionality, however, works, just the "next link" displays horribly wrong, and I can't figure out how to make it display correctly anymore.
sigh
Thanx
Phil