<?php
class page{
public $pageSize; //每页记录数
public $rsTotal; //总记录数
public $pageTotal;
public $nowpage; //当前页
public $offset; //limit偏移量:limit 0,20
public $url;
public $prePage = 'page';
public $pageInfo;
public $pageHtml; //分页连接:首页 上一页 下一页 末页
function __construct($pageSize=20,$rsTotal){
$this->pageSize = $pageSize;
$this->rsTotal = $rsTotal;
$this->pageTotal = ceil($rsTotal/$pageSize);
$this->nowPage = $this->getNowPage();
$this->offset = $this->getOffset();
$this->url = $this->getUrl();
$this->pageInfo = $this->pageInfo();
$this->pageHtml = $this->pageHtml($this->pageInfo);
}
//获取当前页码
private function getNowPage(){
$nowPage = isset($_GET[$this->prePage])?$this->f_Get($this->prePage):1;
$nowPage = $nowPage<1 ? 1 : $nowPage;
$nowPage = $nowPage>$this->pageTotal ? $this->pageTotal : $nowPage;
return $nowPage;
}
//获取当前页的记录偏移量、
private function getOffset(){
$offset = " limit ".($this->nowPage-1)*$this->pageSize.",".$this->pageSize;
return $offset;
}
//生成翻页html代码
public function pageHtml($pageInfo){
//首页
if(!empty($pageInfo['first'])){
if($this->nowPage=="" || $this->nowPage==1){
$pageHtml = '<span class="page_first">'.$pageInfo['first'].'</span>';
}else{
$pageHtml = '<span class="page_first"><a href="?'.$this->url['first'].'">'.$pageInfo['first'].'</a></span>';
}
}
//上一页
if(!empty($pageInfo['pre'])){
if($this->nowPage=="" || $this->nowPage==1){
$pageHtml .= '<span class="page_pre">'.$pageInfo['pre'].'</span>';
}else{
$pageHtml .= '<span class="page_pre"><a href="?'.$this->url['pre'].'">'.$pageInfo['pre'].'</a></span>';
}
}
//下一页
if(!empty($pageInfo['next'])){
if($this->nowPage >= $this->pageTotal){
$pageHtml .= '<span class="page_next">'.$pageInfo['next'].'</span>';
}else{
$pageHtml .= '<span class="page_next"><a href="?'.$this->url['next'].'">'.$pageInfo['next'].'</a></span>';
}
}
//末页
if(!empty($pageInfo['last'])){
if($this->nowPage >= $this->pageTotal){
$pageHtml .= '<span class="page_last">'.$pageInfo['last'].'</span>';
}else{
$pageHtml .= '<span class="page_last"><a href="?'.$this->url['last'].'">'.$pageInfo['last'].'</a></span>';
}
}
if(!empty($pageInfo['t_page'])){
$pageHtml .= '<span class="t_page">'.str_replace("%i%",$this->pageTotal,$pageInfo['t_page']).'</span>';
}
if(!empty($pageInfo['nowPage'])){
$pageHtml .= '<span class="now_page">'.str_replace("%i%",$this->nowPage,$pageInfo['nowPage']).'</span>';
}
if(!empty($pageInfo['r_total'])){
$pageHtml .= '<span class="r_page">'.str_replace("%i%",$this->rsTotal,$pageInfo['r_total']).'</span>';
}
return $pageHtml;
}
//获取当前URL
private function getUrl(){
$str = '';
$urlStr = $_GET;
$queryString = array();
foreach($urlStr as $key=>$value){
if($key == $this->prePage){
continue;
}
$str = $key."=".$value."&";
}
$nextTmp = $this->nowPage+1;
$preTmp = $this->nowPage-1;
$last = $str.$this->prePage.'='.$this->pageTotal;
$next = $str.$this->prePage.'='.$nextTmp;
$pre = $str.$this->prePage.'='.$preTmp;
$first = $str.$this->prePage.'=1';
$queryString = array("pre"=>$pre,"next"=>$next,"last"=>$last,"first"=>$first);
return $queryString;
}
private function pageInfo(){
return array("first"=>"首页","last"=>"末页","next"=>"下一页","pre"=>"上一页","t_page"=>"共 %i% 页","nowPage"=>"当前第 %i% 页","r_toal"=>"共 %i% 条");
}
//过滤函数
private function f_Get($GET){
$GET = $_GET[$GET];
return preg_replace("/[^0-9]/i","",$GET);
}
}
$page=new Page(20,100);
echo $page->pageHtml;
?>
PHP分页原理
最新推荐文章于 2021-03-21 02:31:13 发布