Home >Backend Development >PHP Tutorial >Good PHP paging class_PHP tutorial
class Pager{
//Generated page number (actually it doesn’t matter)
var $pageNumber='';
//Total number of items to be classified
var $totalItems=0;
//Data connection related
var $conn;
var $sql;
//Display several items per page
var $itemsPerPage=3;
//Total number of pages
var $totalPageNumber=0;
//Current page number!
var $currentPageNumber=1;
//Several page numbers displayed on one page
var $length=10;
//URL that needs pagination
var $url='';
function Pager($conn,$sql,$currentPageNumber,$itemsPerPage,$length,$url){
$this->currentPageNumber=$currentPageNumber;
$this->conn=$conn;
$this->sql=$sql;
$this->itemsPerPage=$itemsPerPage;
$this->length=$length;
$this->url=$url;
$this->url.=(stristr($this->url,'?')!=false)?'&':'?'; //If there is "?" in the Url, add "&" if there is no Add "?"
$this->getTotalPageNumber();
}
function getTotalItems(){
//for adodb
$rs=$this->conn->Execute($this->sql);
$this->totalItems=$rs->RecordCount();
return $this->totalItems;
}
function getTotalPageNumber(){
$this->totalPageNumber=ceil($this->getTotalItems()/$this->itemsPerPage);
return $this->totalPageNumber;
}
//LIMIT start in SQL, starting value in length
function getLimitStart(){
$start=($this->currentPageNumber-1)*$this->itemsPerPage;
return $start;
}
//LIMIT start in SQL, length
in length
function getLimitItems(){
return $this->itemsPerPage;
}
function getRsPerPage(){
$modiSQL=$this->sql." limit ".$this->getLimitStart()." ,".$this->getLimitItems();
//I am using adodb
$modiRS=$this->conn->Execute($modiSQL);
$arr=$modiRS->GetArray();
return $arr;
}