Kniffen Web Design

PageNumberer Class

Download
<?php
require_once('class.OptionsObject.php');
 
class PageNumberer
{
	protected $startResult;
	protected $endResult;
	protected $totalResults;
 
	protected $currentPage;
	protected $totalPages;
	protected $startPage;
	protected $endPage;
	protected $pageRangeLength;
 
	protected $baseLink;
	protected $perPage;
 
	protected $opts;
	protected $defaultOpts =  array('maxPages'       => 15,
					'showFirst'      => false, 
					'showLast'       => false, 
					'startType'      => 'pageNumber',
					'startResultVar' => 'startResult');
 
	public function __construct($start, $totalResults, $perPage, $baseLink, $options = array())
	{
		$this->opts = new OptionsObject($this->defaultOpts);
		$this->opts->setOptions($options);
 
		$this->totalResults = $totalResults;
		$this->perPage = $perPage;
		$this->totalPages = ceil($totalResults / $perPage);
 
		if($this->opts->startType == 'pageNumber')
		{
			switch($start)
			{
				case 'first':
					$this->currentPage = 1;
					$this->startResult = 0;
					break;
				case 'last':
					$this->currentPage = $this->totalPages;
					$this->startResult = ($this->currentPage -1) * $this->perPage;
					break;
				default:
					$this->currentPage = intval($start);
					$this->startResult = ($this->currentPage - 1) * $this->perPage;
			}
		}
		else
		{
			$this->currentPage = $start / $this->perPage;
			$this->startResult = $start;
		}
 
		$this->endResult       = min($this->totalResults, ($this->startPage + $this->perPage));
		$this->currentPage     = $this->checkCurrentPage($this->currentPage);
		$this->startPage       = max(1, floor($this->currentPage -($this->opts->maxPages - 1) / 2));
		$this->endPage         = min($this->totalPages, $this->startPage + $this->opts->maxPages -1);
		$this->startPage       = max(1, min($this->startPage, $this->endPage - $this->opts->maxPages + 1));
		$this->baseLink        = $baseLink.'&amp;'.$this->opts->startResultVar.'=';
		$this->currentPageLink = $this->linkIt.$this->startResultp;
						$this->startResult = ($this->currentPage - 1) * $this->perPage;
 
	}
	public function getLinkBar()
	{
	    $display = '';
		if($this->opts->showFirst == true && $this->startPage > 1)
		{
			$display .= '<a href="'.$this->baseLink.'first">First</a>';
		}
		for($x = $this->startPage; $x <= $this->endPage; $x++)
		{
			$display .= '&nbsp;'.$this->getPageLink($x).'&nbsp;';
		}
		if($this->opts->showLast == true && $this->endPage < $this->totalPages)
		{
			$display .= '<a href="'.$this->baseLink.'last">Last</a>';
		}
		return $display;
	}
	public function getPageLink($pageNumber)
	{
		return '<a '.(($pageNumber == $this->currentPage) ? 'class="current" ' : '').
	           'href="'.$this->baseLink.$pageNumber.'">'.$pageNumber.'</a>';
	}
	protected function checkCurrentPage($currentPage)
	{
		$currentPage = intval($currentPage);
		$currentPage = min($currentPage, $this->totalPages);
		$currentPage = max(1, $currentPage);
 
		return $currentPage;
	}
	public function getShowingResults()
	{ 
		return 'Showing Results ('.($this->startResult+1).' - '.$this->endResult.') out of '.$this->totalResults;
	}
	public function getShowingPage()
	{
		return 'Page '.$this->currentPage.' out of '.$this->totalPages;
	}
	public function getCurrentPageLink()
	{ 
		return $this->currentPageLink;
	}
	public function getStartResult(){
		return max(0, $this->startResult);
	}
}
?>
KniffenWebDesign.com