/**
Author: Xiaoxiang Blog
Time:
2009-11-26
php technical group:
37304662
Usage:
include_once'Pager.class.php';
$pager=new Pager();
if(isset($_GET['page']))
$pager->setCurrentPage($_GET ['page']);
else
$pager->setCurrentPage(1);
$pager->setRecorbTotal(1000);
$pager->setBaseUri( "page.php?");
echo $pager->execute();
**/
class Pager{
/**
*int total number of pages
**/
protected $pageTotal;
/**
*intPrevious page
**/
protected $previous;
/**
*intnext page
**/
protected $next;
/**
*int starting sequence number of the middle page
**/
protected $startPage;
/**
*int intermediate page termination sequence number
**/
protected $endPage;
/**
*int total number of records
**/
protected $recorbTotal;
/**
*int displays the number of records per page
**/
protected $pageSize;
/**
*int currently displayed page
**/
protected $currentPage;
/**
*string base url address
**/
protected $baseUri;
/**
*@returnstring Get the base url address
*/
public function getBaseUri(){
return$this->baseUri;
}
/**
*@returnint Get the currently displayed page
*/
public function getCurrentPage(){
return $this->currentPage;
}
/**
*@returnint Get the number of records displayed on each page
*/
public function getPageSize(){
return $this->pageSize;
}
/**
*@returnint Get the total number of records
*/
public function getRecorbTotal(){
return$this->recorbTotal;
}
/**
*@paramstring$baseUri sets the base url address
*/
public function setBaseUri($baseUri){
$this->baseUri=$baseUri;
}
/**
*@paramint$currentPage sets the currently displayed page
*/
public function setCurrentPage($currentPage){
$this->currentPage=$currentPage;
}
/**
*@paramint$pageSize sets the number of records displayed on each page
*/
public function setPageSize($pageSize){
$this->pageSize=$pageSize;
}
/**
*@paramint$recorbTotal setting to get the total number of records
*/
public function setRecorbTotal($recorbTotal){
$this->recorbTotal=$recorbTotal;
}
/**
*Constructor
**/
public function __construct()
{
$this->pageTotal=0;
$this->previous=0;
$this->next=0;
$this->startPage=0;
$this->endPage=0;
$this->pageSize=20;
$this->currentPage=0;
}
/**
*Paging algorithm
**/
private function arithmetic(){
if($this->currentPage<1)
$this->currentPage=1;
$this->pageTotal=floor($this->recorbTotal/$this->pageSize)+($this->recorbTotal%$this->pageSize>0?1:0);
if($this->currentPage>1&&$this->currentPage>$this->pageTotal)
header('location:'.$this->baseUri.'page='.$this->pageTotal);
$this->next=$this->currentPage+1;
$this->previous=$this->currentPage-1;
$this->startPage=($this->currentPage+5)>$this->pageTotal?$this->pageTotal-10:$this->currentPage-5;
$this->endPage=$this->currentPage<5?11:$this->currentPage+5;
if($this->startPage<1)
$this->startPage=1;
if($this->pageTotal<$this->endPage)
$this->endPage=$this->pageTotal;
}
/**
*Pagination style
**/
protected function pageStyle(){
$result="共".$this->pageTotal."页";
if($this->currentPage>1)
$result.="
baseUri."page=1">第1页 baseUri."page=$this->previous">前一页";
else
$result.="
第1页 ";
for($i=$this->startPage;$i<=$this->endPage;$i++){
if($this->currentPage==$i)
$result.="
$i";
else
$result.="
baseUri."page=$i">$i ";
}
if($this->currentPage!=$this->pageTotal){
$result.="
baseUri."page=$this->next">后一页 ";
$result.="
baseUri."page=$this->pageTotal">最后1页";
}else{
$result.="
最后1页 ";
}
return $result;
}
/**
*Perform paging
**/
public function execute(){
if($this->baseUri!=""&&$this->recorbTotal==0)
return"";
$this->arithmetic();
return $this->pageStyle();
}
}
?>