<?php
/*
author:vimal peramangalath
email:hide@address.com
*/
class Pagination
{
private $TotalPages;
private $TotalRows;
private $RowPerPage;
private $CurrentPage;
private $Url;
function pagination($url="#",$total_rows=0,$rowsPerPage=100)
{
$this->Url=$url;
$this->TotalRows=$total_rows;
$this->RowPerPage=$rowsPerPage;
$this->getTotalPages();
}
function displayPageLinks()
{
$density=$this->findDensity(5);
$PageLInks="";
if($density["first_page"])
$PageLInks.="<a href= \" $this->Url?page= $density[first_page] \" class='button-blue'><span id=\"first_page\">First</span></a> <span>....</span>";
for($i=(int)$density["start_page"];$i<=(int)$density["end_page"];$i++)
{
if($this->CurrentPage == $i)
$PageLInks.="<span class='currentpage'>$i</span>";
else
$PageLInks.="<a href= \" $this->Url?page=$i\"><span>$i</span></a>";
}
if($density["last_page"])
$PageLInks.="<span>....</span> <a href= \" $this->Url?page= $density[last_page] \" class='button-blue'><span id=\"last_page\">Last</span></a>";
return $PageLInks;
}
function getCurrentStartRecordNo($pageNo=1)
{
if((int)$pageNo < 1)$pageNo=1;
if((int)$pageNo > $this->TotalPages)$pageNo = $this->TotalPages;
$this->CurrentPage = $pageNo;//assigning for display links.
return (int)($this->CurrentPage * $this->RowPerPage) - (int)( $this->RowPerPage);
}
private function getTotalPages()
{
$this->TotalPages = (int)ceil($this->TotalRows/$this->RowPerPage);
}
private function findDensity($densityFactor=5)
{
$start = 1;
$first = $start;
$last = $this->TotalPages;
$cur = $this->CurrentPage;
$end = $last;
((int)($cur+intval($densityFactor))>= $end)?$end=$last:$end=(int)$cur+$densityFactor;
((int)($cur-intval($densityFactor))<= (int)$start)?$start=$first:$start=(int)$cur-$densityFactor;
($first==$start)?$first=(boolean)false:0;
($end==$last)?$last=(boolean)false:0;
return array("first_page"=>$first,"last_page"=>$last,
"start_page"=>$start,"end_page"=>$end);
}
}