Home >Backend Development >PHP Tutorial >PHP simple paging class_PHP tutorial
/**
* Pagination class
* 2011/8/31
* kcj
**/
class Page{
private $total; //Query the total data records
Private $page; //The current page
//private $pagesize; //Number of items displayed on each page
Private $pagenum; //How many pages in total
private $num; //Number of records displayed on each page
Private $offset; //Get the starting offset of the record from the database
Function __construct($total,$page=1,$num=5){
$this->total=$total;
$this->page=$page;
$this->num=$num;
$this->pagenum=$this->getPageNum();
$this->offset=$this->getOffset();
}
Private function getPageNum(){
Return ceil($this->total/$this->num);
}
Private function getNextPage(){ //Next page
If($this->page==$this->pagenum){
return false;
return $this->page+1;
}
}
private function getPrevPage(){
If($this->page==1){
return false;
return $this->page-1;
}
}
private function getOffset(){
return ($this->page-1)*$this->num;
}
Private function getStartNum(){
If ($this->total==0) {
return 0;
return $this->offset+1;
}
}
private function getEndNum(){
return min($this->offset+$this->num,$this->total);
}
Public function getPageInfo(){
$pageInfo=array(
"row_total"=>$this->total,
"row_num" =>$this->num,
"page_num" =>$this->getPageNum(),
"current_page"=>$this->page,
"row_offset"=>$this->getOffset(),
"next_page"=>$this->getNextPage(),
"prev_page"=>$this->getPrevPage(),
"page_start"=>$this->getStartNum(),
"page_end" =>$this->getEndNum()
);
Return $pageInfo;
}
}
?>
Excerpted from chaojie2009’s column