_totalRows = $totalRows; $this->_rowsPerPage = $rowsPerPage; $this->_currentPage = $currentPage; } /** The number of pages */ function getNumPages() { return ceil($this->_totalRows / $this->_rowsPerPage); } /** The current offset into the results */ function getOffset() { return $this->_currentPage * $this->_rowsPerPage; } /** Returns a limit clause for SQL statments, based on current page */ function getLimitClause() { return "LIMIT ".$this->_rowsPerPage.",".$this->getOffset(); } /** Show a page selector * Example matrix: (12 pages total, 8 is current page * Output $surrounding $nextprev $firstlast * --------------------------------- ------------ --------- ---------- * < 1 .. 7 8 9 .. 12 > 1 true true * < .. 7 8 9 .. > 1 true false * 1 .. 7 8 9 .. 12 1 false true * < 1 .. 5 6 7 8 9 10 11 12 > 3 true true * < 1 .. 6 7 8 9 10 11 12 > 2 true true * < 1 2 3 4 5 6 7 8 9 10 11 12 > false true true * * * @param string $urlprefix Should be of the form "file.php?page=". The page * number will be concatenated on to the end * @param int $surrounding Controls blanking out of page numbers. If False, * all page numbers are shown. If any integer, then * that number of page numbers are shown next to * the current page, and the rest are seperated with "..." * @param bool $nextprev True to show < and > arrows at the ends of the string, * to link to previous/next page * @param bool $firstlast True to always show a number for the first and last * pages, regardless of $surrounding setting. This has * no effect if $surrounding = false, since all pages * are shown anyways. */ function showPager($urlprefix, $surrounding = false, $nextprev = true, $firstlast = true) { $out = ""; if ($nextprev && ($this->_currentPage > 1)) { // show previous button, if applicable $out .= "_currentPage - 1)."\">< "; } $total = $this->getNumPages(); for ($i = 1; $i <= $total; $i++) { $cond_showall = false; $cond_notblocked = false; $cond_firstlast = false; //echo " [$i]"; // not blocking any numbers, so show them all $cond_showall = ($surrounding === false); //echo ($cond_showall ? "1" : "1"); if (!$cond_showall) { // if we are blocking, is this within +/- $surrounding numbers from current page? $cond_notblocked = (abs($this->_currentPage - $i) <= $surrounding); //echo ($cond_notblocked ? "2" : "2"); if ($cond_notblocked == false) { // its blocked, lets check if it should be unblocked if ($firstlast) { //$cond_notblocked = ($i <= 2); } else { } } // if using $firstlast, then if this is the first or last, show it $cond_firstlast = ($firstlast && (($i == 1) || ($i == $total))); //echo ($cond_firstlast ? "3" : "3"); } if ($cond_showall || $cond_notblocked || $cond_firstlast) { if ($this->_currentPage == $i) { $out .= $i." "; } else { $out .= "".$i." "; } } else { $out .= "."; } } if ($nextprev && ($this->_currentPage < $total)) { // show next button, if applicable $out .= "_currentPage + 1)."\">> "; } // replace all our non-numbers with just "..." // probably not the most efficient way, but works well $out = preg_replace("/\.+/","... ",$out); return $out; } } echo "

showPager(surrounding = 2, nextprev = true, firstlast = true)
"; for ($i = 1; $i < 13; $i++) { $pager = new tablepager(115, 10, $i); echo $pager->showPager("?page=", 2, true, true)."
"; } echo "

showPager(surrounding = 2, nextprev = true, firstlast = false)
"; for ($i = 1; $i < 13; $i++) { $pager = new tablepager(115, 10, $i); echo $pager->showPager("?page=", 2, true, false)."
"; } echo "

showPager(surrounding = false, nextprev = true, firstlast = true)
"; for ($i = 1; $i < 13; $i++) { $pager = new tablepager(115, 10, $i); echo $pager->showPager("?page=", false, true, true)."
"; } ?>