This is the full pagination class I posted previously.
You should see not only does it handle calculations but builds the menus using methods such as: makeMenu(), makeDropDown() and dropDownJavascript().
The makeMenu() and makeDropDown() methods in particular accept a string as a url and replace {page} with the page for the link. The class is also responsible for determining which links are active, inactive and current.
All this can be accomplished without dependencies on a model. Its just a numbers game.
PHP Code:
<?php
class Pagination {
// total number of records per page
private $_count;
// offset from beginning
private $_offset;
// current page
private $_page;
// total number of records with pagination
private $_total;
// number of controls to show from current page
private $_range = 1;
public function __construct($pCount,$pTotal,$pPage) {
$this->_count = $pCount?$pCount:10;
$this->_total = $pTotal;
$this->_page = $pPage?$pPage:1;
$this->_currentPage();
$this->_currentOffset();
}
private function _currentOffset() {
$page = $this->_page;
$this->_offset = $this->_page==1?0:$this->_count*(--$page);
}
private function _currentPage() {
// send user to last page if they have gone past the total number of pages in the path variable
if($this->_page>$this->pages()) {
$this->_page = $this->pages();
}
}
// number of pages
public function pages() {
/*
* if the count excedes the the total number than set
* the default to 1 page. There are less records than
* will fit on one whole page.
*/
return $this->_count>=$this->_total?1:ceil($this->_total/$this->_count);
}
public function getOffset() {
return $this->_offset;
}
public function getCount() {
return $this->_count;
}
public function getPage() {
return $this->_page;
}
public function getTotal() {
return $this->_total;
}
public function getFirstPage() {
return 1;
}
public function firstPageActive() {
return $this->getFirstPage() == $this->getPage()?false:true;
}
public function getPreviousPage() {
return $this->getPage()-1;
}
public function hasPreviousPage() {
$previousPage = $this->getPreviousPage();
return $previousPage>=1?true:false;
}
public function previousPageActive() {
return $this->hasPreviousPage();
}
public function getNextPage() {
return $this->getPage()+1;
}
public function hasNextPage() {
$nextPage = $this->getNextPage();
return $nextPage <= $this->pages()?true:false;
}
public function nextPageActive() {
return $this->hasNextPage();
}
public function getLastPage() {
return $this->pages();
}
public function lastPageActive() {
return $this->getLastPage() == $this->getPage() ? false : true;
}
public function getRange() {
$min = $this->getPage()-1;
$max = $this->getPage()+1;
if($min<1) {
$min=1;
}
if($max>$this->pages()) {
$max=$this->pages();
}
return range($min,$max,1);
}
public function firstPageLink($pHref,$pLabel='First') {
$href = preg_replace('/{page}/',$this->getFirstPage(),$pHref);
return '<a class="active" href="'.$href.'">'.$pLabel.'</a>';
}
public function firstPageButton($pHref,$pLabel='First') {
if($this->firstPageActive()===true) {
return $this->firstPageLink($pHref,$pLabel);
} else {
return '<span class="inactive">'.$pLabel.'</span>';
}
}
public function previousPageLink($pHref,$pLabel='Previous') {
$href = preg_replace('/{page}/',$this->getPreviousPage(),$pHref);
return '<a class="active" href="'.$href.'">'.$pLabel.'</a>';
}
public function previousPageButton($pHref,$pLabel='Previous') {
if($this->previousPageActive()===true) {
return $this->previousPageLink($pHref,$pLabel);
} else {
return '<span class="inactive">'.$pLabel.'</span>';
}
}
public function nextPageLink($pHref,$pLabel='Next') {
$href = preg_replace('/{page}/',$this->getNextPage(),$pHref);
return '<a class="active" href="'.$href.'">'.$pLabel.'</a>';
}
public function nextPageButton($pHref,$pLabel='Next') {
if($this->nextPageActive()===true) {
return $this->nextPageLink($pHref,$pLabel);
} else {
return '<span class="inactive">'.$pLabel.'</span>';
}
}
public function lastPageLink($pHref,$pLabel='Last') {
$href = preg_replace('/{page}/',$this->getLastPage(),$pHref);
return '<a class="active" href="'.$href.'">'.$pLabel.'</a>';
}
public function lastPageButton($pHref,$pLabel='Last') {
if($this->lastPageActive()===true) {
return $this->lastPageLink($pHref,$pLabel);
} else {
return '<span class="inactive">'.$pLabel.'</span>';
}
}
public function pageLinks($pHref,$pHighlightCurrent=true,$pCurrentClass='current') {
$str = '<ul class="pages">'."\n";
foreach($this->getRange() as $range) {
if($pHighlightCurrent===true && $range==$this->getPage()) {
$str.= '<li><span class="'.$pCurrentClass.'">'.$range.'</span></li>'."\n";
} else {
$href = preg_replace('/{page}/',$range,$pHref);
$str.= '<li><a href="'.$href.'">'.$range.'</a></li>'."\n";
}
}
return $str."\n".'</ul>';
}
public function firstPreviousModule($pHref) {
$str = '<ul>'."\n";
$str.= "\t".'<li>'.$this->firstPageButton($pHref).'</li>'."\n";
$str.= "\t".'<li>'.$this->previousPageButton($pHref).'</li>'."\n";
$str.= '</ul>'."\n";
return $str;
}
public function nextLastModule($pHref) {
$str = '<ul>'."\n";
$str.= "\t".'<li>'.$this->nextPageButton($pHref).'</li>'."\n";
$str.= "\t".'<li>'.$this->lastPageButton($pHref).'</li>'."\n";
$str.= '</ul>'."\n";
return $str;
}
public function makeMenu($pHref,$attr='') {
$str = '<ul'.$attr.'>'."\n";
$str.= '<li>'."\n";
$str.= $this->firstPreviousModule($pHref);
$str.= '</li>'."\n";
$str.= '<li>'."\n";
$str.= $this->pageLinks($pHref);
$str.= '</li>'."\n";
$str.= '<li>'."\n";
$str.= $this->nextLastModule($pHref);
$str.= '</li>'."\n";
$str.= '</ul>'."\n";
return $str;
}
public function makeDropDown($pHref,$pId) {
$pages = $this->pages();
$str = '<select id="'.$pId.'">'."\n";
for($i=1;$i<=$pages;$i++) {
$value = preg_replace('/{page}/',$i,$pHref);
if($this->getPage()==$i) {
$str.= "\t".'<option value="'.$value.'" selected="selected">'.$i.'</option>'."\n";
} else {
$str.= "\t".'<option value="'.$value.'">'.$i.'</option>'."\n";
}
}
$str.= '</select>'."\n";
return $str;
}
public function dropDownJavascript($pId) {
?>
var el = document.getElementById('<?php echo $pId ?>');
el['onclick'] = function(pEvt) {
var e = pEvt || event;
target = e.target || e.srcElement;
if(target.nodeName!='OPTION') { return; }
window.location = target.getAttribute('value');
}
<?php
}
}
?>
Bookmarks