Home  >  Article  >  Backend Development  >  A relatively simple PHP paging grouping class_PHP tutorial

A relatively simple PHP paging grouping class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:30745browse

Copy code The code is as follows:

class mysqlPager{
var $pagePerNum=5;//Number of data items displayed on each page
var $pagePerGroup=5;//Number of pages in each 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="";//Currently used paging URL
var $custom;//Custom style
var $pageQuerySql= "";
function mysqlPager(){//Constructor PHP4
}
/**
* Initialize all variables
*/
function InitAllVar($totalNum,$pagePerGroup,$curPageUrl,$curPage=1 ,$curPageGroup=1)
{
$this->totalNum=$totalNum;
$this->pagePerGroup=$pagePerGroup;
$this->curPageUrl=$curPageUrl;
$this->curPage=$curPage;
$this->curPageGroup=$curPageGroup;
}
/**
* Set the current page variable
*
* @param number $curPage
*/
function setCurPage($curPage)
{
$this->curPage=$curPage;
}
/**
* Set the current paging group variable
*
* @param mixed $curPageGroup
*/
function setCurPageGroup($curPageGroup)
{
$this- >curPageGroup=$curPageGroup;
}
/**
* Set the URL of the currently used distribution class
* $curPageUrl string
*/
function setCurPageUrl($curPageUrl)
{
$this->curPageUrl=$curPageUrl;
}
/**
* Get all
*
* @param number $totalNum
* @param number $curPage
* @return float
*/
function getTotalPage($totalNum,$curPage=0)
{
return $this->totalPage=ceil($totalNum/$this- >pagePerNum);
}
/**
* Set user-defined style
*
* @param mixed $customStyle
*/
function setCustomStyle($customStyle)
{
$this->customStyle=$customStyle;
}
/**
* Set user-defined style return string
*
*
* @param mixed $pagerString
*/
function setCustomStyleString($pagerString)
{
return $styleString="".$pagerString. "";
}
/**
* You can output navigation page information without parameters, but you must set the corresponding variables before use
*
* @param mixed $curPageGroup
* @param mixed $curPage
* @param mixed $curPageUrl
*/
function showNavPager($curPageGroup=0,$curPage=0,$curPageUrl=0)
{
if($curPageGroup)
{
$this->curPageGroup=$curPageGroup;
}
if($curPage)
{
$this->curPage=$ curPage;
}
if($curPageUrl)
{
$this->curPageUrl=$curPageUrl;
}
$rtnString="";
//Judgement Whether the 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.="".$this->setCustomStyleString("<<")." " ;
}
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.=" curPageUrl?cpg={$ this->curPageGroup}&cp=$curPageNum >";
$rtnString.=$this->setCustomStyleString($curPageNum)."
";
}
}
}
if($this->curPageGrouptotalPage/$this->pagePerGroup)-1)
{
$nextPageGroup=$this->curPageGroup+ 1;
$rtnString.=" curPageUrl?cpg=$nextPageGroup >".$this->setCustomStyleString(">>")."}
$this->pageQuerySql=" limit ".(($this->curPage-1)*$this->pagePerNum).",".$this-> pagePerNum;

}
else
{
$rtnString="Error: Variable not initialized! ";
}
return $rtnString;
}
/**
* Get the complete Sql statement for querying MYSQL
*
* @param mixed $sql
*/
function getQuerySqlStr($sql)
{
$allsql=$sql. $this->pageQuerySql;
return $allsql;
}
/**
* Set how many data items there are on each page
*
* @param INT $num
*/
function setPagePerNum($num)
{
$this-> pagePerNum=$num;
}
}
?>
Usage:
$curPage=$_GET['cp'];
$curPageGroup=$_GET['cpg ']
if($curPage=="")
$curPage=1;
if($curPageGroup=="")
$curPageGroup=1;
//All from 1. Before starting, the incoming data must be verified to prevent injection
//.
$pager=new MysqlPager();
$pager->initAllVar(...)
$pager->showNavPager();
//The following SQL can be any output
$sql="select id form dbname ";
$querysql=$pager->getQuerySqlStr($sql)
//You can use $querysql to query the database in the future to get the corresponding result set

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320953.htmlTechArticleCopy the code as follows: ?php class mysqlPager{ var $pagePerNum=5;//The number of data items displayed on each page var $pagePerGroup=5;//Number of pages in each paging group var $curPage=0;//Current page, Defualt first...