PHP Google or baidu paging code
- 2020-03-31 17:10:16
- OfStack
<?php
class Pager{
protected $pageTotal;
protected $previous;
protected $next;
protected $startPage;
protected $endPage;
protected $recorbTotal;
protected $pageSize;
protected $currentPage;
protected $baseUri;
public function getBaseUri(){
return$this->baseUri;
}
public function getCurrentPage(){
return $this->currentPage;
}
public function getPageSize(){
return $this->pageSize;
}
public function getRecorbTotal(){
return$this->recorbTotal;
}
public function setBaseUri($baseUri){
$this->baseUri=$baseUri;
}
public function setCurrentPage($currentPage){
$this->currentPage=$currentPage;
}
public function setPageSize($pageSize){
$this->pageSize=$pageSize;
}
public function setRecorbTotal($recorbTotal){
$this->recorbTotal=$recorbTotal;
}
public function __construct()
{
$this->pageTotal=0;
$this->previous=0;
$this->next=0;
$this->startPage=0;
$this->endPage=0;
$this->pageSize=20;
$this->currentPage=0;
}
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;
}
protected function pageStyle(){
$result=" A total of ".$this->pageTotal." page ";
if($this->currentPage>1)
$result.="<a href="".$this->baseUri."page=1"><font style="font-family:webdings"> The first 1 page </font></a> <a href="".$this->baseUri."page=$this->previous"><fontstyle="font-family:webdings"> The previous page </font></a>";
else
$result.="<font style="font-family:webdings"> The first 1 page </font> <font style="font-family:webdings"></font>";
for($i=$this->startPage;$i<=$this->endPage;$i++){
if($this->currentPage==$i)
$result.="<font color="#ff0000">$i</font>";
else
$result.=" <a href="".$this->baseUri."page=$i">$i</a> ";
}
if($this->currentPage!=$this->pageTotal){
$result.="<a href="".$this->baseUri."page=$this->next"><font style="font-family:webdings"> After a page </font></a> ";
$result.="<a href="".$this->baseUri."page=$this->pageTotal"><font style="font-family:webdings"> The last 1 page </font></a>";
}else{
$result.="<font style="font-family:webdings"> The last 1 page </font> <font style="font-family:webdings"></font>";
}
return $result;
}
public function execute(){
if($this->baseUri!=""&&$this->recorbTotal==0)
return"";
$this->arithmetic();
return $this->pageStyle();
}
}
?>