Location: PHPKode > scripts > Page Navigator - with median > PageNavigation.Class.php
<?php
/**
 * \brief This class is used for calculating the Page Navigation link
 * \author Dhananjay
 * \version 1.0
 * \date 06-May-2010 02:19:01 PM
 */
 
class PageNavigation {
	private $su_page_median = 3; #set your median
	private $su_page_limit = 10; #set you page limit per page to show
	
	public function __construct(){
	
	}
	
	public function __destruct(){
	
	}
	
	/**
	* \note \b This function is used for getting the page navigation
	* 
	* \param $pageno, $total, $fileName - currentpage/ noofdata / filename to call next url
	*
	* \return array
	*/	
	public function getPageNavigation($pageno, $total, $fileName){ 
		$aPageDetails = $this->calculatePageNavigation($pageno, $total, $fileName); 
		return $aPageDetails;
	}
	 
	/**
	* \note \b This function is used for calculate the pagination
	* 
	* \param $pageno, $total, $fileName - currentpage/ noofdata / filename to call next url
	*
	* \return array
	*/
	
	private function calculatePageNavigation($pageno, $total, $fileName){ 
		$median = $this->su_page_median;
		$limit = $this->su_page_limit;  
		$temp = (($pageno>0)?$pageno:1);
		$iTotalRem = $total % $limit ;#getting remainder of total page by limit 
		$numPage = ceil($total / $limit );
		
		#error handling if page requested by user is greater than no. of total pages, it will show first records
		$temp = ($temp > $numPage?1:$temp);
		
		$prev =($temp-1);
		$startpos = ($temp-1) * $limit; 	
		$next = 0;
		if ($temp<$numPage) {
			$next=($temp+1);
		}
		
		if ($temp==$numPage && $iTotalRem>0){
			$limit = $iTotalRem; //is last page and current page same
		}
		
		//Calculating - median page to show - start
			#median - start
			if ($temp > $median){
				$showPageStart = $temp-$median < 1?0:$temp-$median ; 
			}
			else {
				$showPageStart = 1; 
			}
	 
			#median - end  
			if ($numPage > $median){
				$showPageEnd = $temp+$median > $numPage ? $numPage : $temp+$median; 
			}
			else {
				$showPageEnd = $numPage; 
			}  
		//Calculating - median page to show - end
	 
		$j = $showPageStart>0?$showPageStart-1:$showPageStart; 
		if ($numPage>1) {
			for ($i = $showPageStart; $i <= $showPageEnd; $i++){
				$j++;
				if ($temp == $j){
					$pageCurr .= " <span><b>$j</b></span> ";
				} else {
					$pageCurr .= " <a href=\"$fileName=$j\">$j</a> ";
				} 
			}
		}
		
		if ($prev>0){
			$pageFirst = "<a href=\"$fileName=1\">&laquo;&laquo;  first</a> ";
			$pagePrev = "<a href=\"$fileName=$prev\">&laquo; prev</a> ";
		}
		
		if ($next<=$numPage && $next>0){
			$pageNext = " <a href=\"$fileName=$next\">next &raquo;</a> ";
			$pageLast = "<a href=\"$fileName=$numPage\">last &raquo;&raquo;</a> ";
		}
		
		$pageNavigate = $pageFirst.$pagePrev.$pageCurr.$pageNext.$pageLast; 
		$aPageDetails = array('navigation'=>$pageNavigate, 'currentpage'=>$temp, 'startpos'=>$startpos, 'limit'=>$limit, 'noofpages'=>$numPage);
		return $aPageDetails;
	} // getNavigationString
	
}// end of class	
?>
Return current item: Page Navigator - with median