A relatively simple PHP paging grouping class

  • 2020-03-31 17:01:15
  • OfStack


<?php 
class mysqlPager{ 
var $pagePerNum=5;//Displays the number of data items per page
var $pagePerGroup=5;//Number of pages per paging group
var $curPage=0;//Current page, Defualt first page
var $totalPage=0;//Total number of pages
var $totalNum=0;//Total number of data items
var $curPageGroup=0;//Current paging group
var $curPageUrl="";//The URL of the page is currently in use
var $customStyle="";//Custom style
var $pageQuerySql=""; 
function mysqlPager(){//Constructor PHP4
} 
 
function InitAllVar($totalNum,$pagePerGroup,$curPageUrl,$curPage=1,$curPageGroup=1) 
{ 
$this->totalNum=$totalNum; 
$this->pagePerGroup=$pagePerGroup; 
$this->curPageUrl=$curPageUrl; 
$this->curPage=$curPage; 
$this->curPageGroup=$curPageGroup; 
} 
 
function setCurPage($curPage) 
{ 
$this->curPage=$curPage; 
} 
 
function setCurPageGroup($curPageGroup) 
{ 
$this->curPageGroup=$curPageGroup; 
} 
 
function setCurPageUrl($curPageUrl) 
{ 
$this->curPageUrl=$curPageUrl; 
} 
 
function getTotalPage($totalNum,$curPage=0) 
{ 
return $this->totalPage=ceil($totalNum/$this->pagePerNum); 
} 
 
function setCustomStyle($customStyle) 
{ 
$this->customStyle=$customStyle; 
} 
 
function setCustomStyleString($pagerString) 
{ 
return $styleString="<span class=".$customStyle.">".$pagerString."</span>"; 
} 
 
function showNavPager($curPageGroup=0,$curPage=0,$curPageUrl=0) 
{ 
if($curPageGroup) 
{ 
$this->curPageGroup=$curPageGroup; 
} 
if($curPage) 
{ 
$this->curPage=$curPage; 
} 
if($curPageUrl) 
{ 
$this->curPageUrl=$curPageUrl; 
} 
$rtnString=""; 
//Determines whether a variable is initialized
if($this->curPageGroup && $this->curPageUrl && $this->totalNum && $this->curPage) 
{ 
$this->totalPage=$this->getTotalPage($this->totalNum); 
if($this->curPage==1) 
$this->curPage=($this->curPageGroup-1)*$this->pagePerGroup+1; 
if($this->curPageGroup!=1) 
{ 
$prePageGroup=$this->curPageGroup-1; 
$rtnString.="<a href=".$this->curPageUrl."?cpg=$prePageGroup >".$this->setCustomStyleString("<<")."</a> "; 
} 
for($i=1;$i<=$this->pagePerGroup;$i++) 
{ 
$curPageNum=($this->curPageGroup-1)*$this->pagePerGroup+$i; 
if($curPageNum<=$this->totalPage){ 
if($curPageNum==$this->curPage) 
{ 
$rtnString.=" ".$this->setCustomStyleString($curPageNum); 
}else 
{ 
$rtnString.=" <a href=$this->curPageUrl?cpg={$this->curPageGroup}&cp=$curPageNum >"; 
$rtnString.=$this->setCustomStyleString($curPageNum)."</a>"; 
} 
} 
} 
if($this->curPageGroup<ceil($this->totalPage/$this->pagePerGroup)-1) 
{ 
$nextPageGroup=$this->curPageGroup+1; 
$rtnString.=" <a href=$this->curPageUrl?cpg=$nextPageGroup >".$this->setCustomStyleString(">>")."</a>"; 
} 
$this->pageQuerySql=" limit ".(($this->curPage-1)*$this->pagePerNum).",".$this->pagePerNum; 

} 
else 
{ 
$rtnString=" Error: variable not initialized! "; 
} 
return $rtnString; 
} 
 
function getQuerySqlStr($sql) 
{ 
$allsql=$sql.$this->pageQuerySql; 
return $allsql; 
} 
 
function setPagePerNum($num) 
{ 
$this->pagePerNum=$num; 
} 
} 
?> 
 Usage:  
$curPage=$_GET['cp']; 
$curPageGroup=$_GET['cpg'] 
if($curPage=="") 
$curPage=1; 
if($curPageGroup=="") 
$curPageGroup=1; 
//Start from 1, before the incoming data to verify, anti - injection
// .  
$pager=new MysqlPager(); 
$pager->initAllVar(...) 
$pager->showNavPager(); 
//The following SQL can be arbitrary output
$sql="select id form dbname "; 
$querysql=$pager->getQuerySqlStr($sql) 
//Later query the database with $querysql to get the corresponding result set

Related articles: