PHPPageNo Class For Easy Page Numbering Creation

PHP

Brief

PHPPageNo Class helps you to implement page numbering in your PHP application.
This class shows Page No with links to navigate the Page’s Url.
The class constructor have four value settings that you can adjust like $startRec which represents starting record,
$totalRec is total records, $maxRecPerPage is a maximum no of records per page and $maxPageDisplay is a maximum no of page shown.
Anyway you should understand basic of OOP in PHP like constructor, class extends etc in order to get the idea of this post.

The screen shot of how PHPPageNo works is below:

Resulted Page No are also includes Prev and Next links if the total page query is more than $maxPageDisplay.
This makes the page numbering would not be shown like blown list of page no.

PHPPageNo Class

class PHPPageNo {
	protected $startRec;
	protected $totalRec;
	
	//Maximum record per Page
	protected $maxRecPerPage;
	
	//Maximum page per Display
	protected $maxPageDisplay;	
	protected $totalPage;
	protected $curPage;
	
	protected $urlflag;
	
	function __construct($startRec,$totalRec,$maxRecPerPage,$maxPageDisplay) {
		$this->startRec = $startRec;
		$this->totalRec = $totalRec;
		$this->maxRecPerPage = $maxRecPerPage;
		$this->maxPageDisplay = $maxPageDisplay;
		$this->curPage=ceil(($this->startRec+1)/$this->maxRecPerPage);
		$this->totalPage=ceil($this->totalRec/$this->maxRecPerPage);
		$this->urlflag = "0";
	}
	
	function set_urlflag() {
		$this->urlflag = "1";
	}
	protected function pageno_url($rec) {
		if ($this->urlflag == "1") {
			$url = "?lnkpage=1&strec=".$rec;
		} else {
			$url = "?strec=".$rec;
		}
		return $url;
	}	
	
	protected function makeLink($str,$url,$bold="false"){
		if($bold){
			$str="<b>".$str."</b>";
			return '<a href="'.$url.'" style="color:red;font-size:10pt;">'.$str.'</a>';
		} else {
			return '<a href="'.$url.'" style="color:blue;font-size:10pt;">'.$str.'</a>';
		}		
	}
	
	protected function makeImgLink($imgpath,$url) {
		return '<a href="'.$url.'"><img src="'.$imgpath.'" style="width:13px;height:13px" border=0/></a>';
	}

	//Generating Page No
	function build(){
		
		$str="";
		
		$endpage = ceil($this->curPage/$this->maxPageDisplay)*$this->maxPageDisplay;
		$startpage = $endpage - ($this->maxPageDisplay -1);
		
		/*--------------------------prev button-----------------------*/
		if($this->curPage > $this->maxPageDisplay){	
			if ($this->curPage % $this->maxPageDisplay <> 0 ) {
				$prevPage = (floor($this->curPage / $this->maxPageDisplay)) * $this->maxPageDisplay;
			} else {
				$prevPage = (floor($this->curPage / $this->maxPageDisplay)-1) * $this->maxPageDisplay;
			}
			$rec = ($prevPage-1) * $this->maxRecPerPage;
			$str.=" ".$this->makeLink("Prev",$this->pageno_url($rec), false)." ";			
		}
		
		/*-------------------------generate page number----------------*/
		
		
		for($i=$startpage;$i<=$endpage;$i++){			
			if ($i <= $this->totalPage) {
				$rec=($i-1)*$this->maxRecPerPage;	
				if($i==$this->curPage){
					$bold=true;
				}else{
					$bold=false;
				}			
				$str.=" &nbsp;".$this->makeLink($i,$this->pageno_url($rec),$bold)."&nbsp; ";
			} 
		}
		
		/*--------------------------next button-----------------------*/
		
		if($this->curPage <= floor($this->totalPage/$this->maxPageDisplay)*$this->maxPageDisplay && 
			$endpage < $this->totalPage){ //&& $this->totalPage > $this->maxPageDisplay	
			$nextPage = (ceil($this->curPage/$this->maxPageDisplay)*$this->maxPageDisplay) + 1;		
			$rec = ($nextPage-1) * $this->maxRecPerPage;
			$str.=" ".$this->makeLink("Next",$this->pageno_url($rec), false)." ";			
		}
		
		return $str;
		
	}
	
	//get current page
	function get_curPage() {
		return $this->curPage;
	}
	
	//next page link
	function write_nextLink($imgpath='') {
		$rec=($this->curPage*$this->maxRecPerPage);	
		if ($imgpath == '') {
			return $this->makeLink(">",$this->pageno_url($rec), false);
		} else {
			return $this->makeImgLink($imgpath, $this->pageno_url($rec));
		}
	}
	
	//previous page link
	function write_prevLink($imgpath='') {
		$rec=($this->curPage-2)*$this->maxRecPerPage;					
		if ($imgpath == '') {
			return $this->makeLink("<",$this->pageno_url($rec), false);
		} else {
			return $this->makeImgLink($imgpath, $this->pageno_url($rec));
		}
		
	}
	
	//First Page link
	function write_firstLink($imgpath='') { 	
		if ($imgpath == '') {
			return $this->makeLink("|<",$this->pageno_url(0), false);
		} else {
			return $this->makeImgLink($imgpath, $this->pageno_url(0));
		}
	}
	
	//Last Page	Link
	function write_lastLink($imgpath='') { 
		$rec=(($this->totalPage-1)*$this->maxRecPerPage);	
		
		if ($imgpath == '') {		
			return $this->makeLink(">|",$this->pageno_url($rec), false);
		} else {
			return $this->makeImgLink($imgpath, $this->pageno_url($rec));
		}
	}
	
	//get total page
	function get_totalPage() {
		return $this->totalPage;
	}
	
	function get_totalRec() {
		return $this->totalRec;
	}
}

Some procedures can show an image to navigate paging instead of using number i.e write_nextLink($imgpath=''), write_prevLink($imgpath=''), etc.

The PHPPageNo class is also extendable to suit with your PHP application. You can overload the Url function pageno_url($url).
If Your app used Physical Path Url to indicate paging then you can use default procedures but maybe your app used javascript like ajax JQuery to do paging and hence you need to extend PHPPageNo.
In order to use javascript to navigate Page url, you need to overload pageno_url($rec).

However, Firstly I give you the default PHPPageNo implementation.

Default Implementation

	include_once "PHPPageNo.php";
	
	$fruits = array("apple", "grape", "banana", "pineaple", "melon",
				"watermelon", "orange", "soursop", "thorny palm", "mangosteen",
				"dragon fruit", "ambarella", "papaya", "mango", "guava",
				"durian", "pear", "date palm", "blueberry", "avocado",
				"figs","kiwi","pomegranate");
	
	/*--- Constructor Value Settings */
	$maxRecPerPage = 5;
	if (isset($_GET['strec'])) {
		$startRec = ($_GET['strec']==null)?"0":$_GET['strec'];	
	} else {
		$startRec = "0";
	}
	$maxPageDisplay = 10;	
	$totalRec = count($fruits);
	/*--- End Value Settings */
	
	echo "<h2>PHPPageNo Example</h2>";
	echo "<table>";
	echo "<tr><td><b>No</b></td><td><b>Fruit</b></td></tr>";
	for ($i = $startRec;$i<$startRec+$maxRecPerPage;$i++) {
		if ($i>=$totalRec) break;
		echo "<tr><td>".($i+1)."</td><td>$fruits[$i]</td></tr>";
	}
	echo "</table>";
	
	/*--- PHPPageNo to generate Page No using build() procedure */
	$clsPageNo = new PHPPageNo($startRec,$totalRec,$maxRecPerPage,$maxPageDisplay);
	echo "<b>Go to Page:</b> ".$clsPageNo->build();
	
	// Use image to show Page No as image link
	echo "<br><b>Using Image:</b> ".$clsPageNo->write_firstLink("firstpage.gif")." ".$clsPageNo->write_prevLink("prevpage.gif")." ".
		$clsPageNo->write_nextLink("nextpage.gif")." ".$clsPageNo->write_lastLink("lastpage.gif");
	/*--- End PHPPageNo */

The PHPPageNo implementation result screen shot:

There is no Next or Prev links because of the total page is less then $maxPageDisplay value which is 4 compared to 10 page.
You can simulate to change the setting to $maxPageDisplay = 2;. With this setting then the total page is more then $maxPageDisplay value and here is the pic.

PHPPageNo Extended

Assuming your PHP app is using ajax JQuery for navigating page no Url then you need to extend PHPPageNo class.
Ultimately to overload pageno_url($rec) function.

	include_once "PHPPageNo.php"; 
	class PHPPageNo_Extended extends PHPPageNo {
		function __construct($startRec,$totalRec,$maxRecPerPage,$maxPageDisplay) {
			parent::__construct($startRec,$totalRec,$maxRecPerPage,$maxPageDisplay);
		}
		
		function pageno_url($rec) {
			$url = "javascript:showContent('".$rec."','".$this->maxRecPerPage."','".$this->maxPageDisplay."')";
			return $url;
		}	
	}

Now, The page No Url uses javascript function instead of physical Url path.

The PHPPage_Extended implementation, HTML file using JQuery:

<html>
<head>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		var startRec = "0";
		var maxRecPerPage = "5";
		var maxPageDisplay= "2";
		showContent(startRec, maxRecPerPage, maxPageDisplay);
	});
	function showContent(startRec, maxRecPerPage, maxPageDisplay) {
		$.ajax({
			data:"strec=" + startRec + "&maxrecperpage=" + maxRecPerPage + "&maxpagedisplay=" + maxPageDisplay,
			type:"get",
			url:"phpcontent.php",
			success: function(response) {
				$("#div_content").html(response);
			}
		});
	}
</script>
</head>
<body>
<div id="div_content"></div>

</body>
</html>

The PHPPage_Extended implementation, PHP file to show page’s content:

	include_once "PHPPageNo_Extended.php"; 
	$fruits = array("apple", "grape", "banana", "pineaple", "melon",
				"watermelon", "orange", "soursop", "thorny palm", "mangosteen",
				"dragon fruit", "ambarella", "papaya", "mango", "guava",
				"durian", "pear", "date palm", "blueberry", "avocado",
				"figs","kiwi","pomegranate");
	
	/*--- Constructor Value Settings */
	$maxRecPerPage = $_GET["maxrecperpage"];
	if (isset($_GET['strec'])) {
		$startRec = ($_GET['strec']==null)?"0":$_GET['strec'];	
	} else {
		$startRec = "0";
	}
	$maxPageDisplay = $_GET["maxpagedisplay"];	
	$totalRec = count($fruits);
	/*--- End Value Settings */
	
	echo "<h2>PHPPageNo Extended Example</h2>";
	echo "<table>";
	echo "<tr><td><b>No</b></td><td><b>Fruit</b></td></tr>";
	for ($i = $startRec;$i<$startRec+$maxRecPerPage;$i++) {
		if ($i>=$totalRec) break;
		echo "<tr><td>".($i+1)."</td><td>$fruits[$i]</td></tr>";
	}
	echo "</table>";
	
	/*--- PHPPageNo_Extended to generate Page No using build() procedure */
	$clsPageNo = new PHPPageNo_Extended($startRec,$totalRec,$maxRecPerPage,$maxPageDisplay);
	echo "<b>Go to Page:</b> ".$clsPageNo->build();
	echo "<br><b>Using Image:</b> ".$clsPageNo->write_firstLink("firstpage.gif")." ".$clsPageNo->write_prevLink("prevpage.gif")." ".
		$clsPageNo->write_nextLink("nextpage.gif")." ".$clsPageNo->write_lastLink("lastpage.gif");
	/*--- End PHPPageNo_Extended */

Almost no change on implementation side compared to default one. The only difference is we called PHPPageNo_Extended class to match your PHP app.


As you can see on this second example, the page Url become javascript due to using extended PHPPageNo.

Cheers,
Agung Gugiaji

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s