<?php
//自定义分页类
class Page
{
public $page=1; //当前页
public $pageSize=5; //页大小
public $maxPage=0; //最大页数
public $maxRows=0; //最大数据条
public $url =null; //访问的url地址
public $params=null; //url的参数条件
public function __construct($maxRows,$pageSize=5){
$this->maxRows = $maxRows;
$this->pageSize= $pageSize;
//获取当前页
$this->page = isset($_GET['p'])?$_GET['p']:1;
$this->getMaxPage();
$this->checkPage();
//获取和封装url地址
$this->url = $_SERVER["PHP_SELF"];
$this->getParams();
}
//获取分页信息
public function limit(){
return (($this->page-1)*$this->pageSize).",".$this->pageSize;
}
//计算分页中的最大页数
private function getMaxPage(){
$this->maxPage = ceil($this->maxRows/$this->pageSize);
}
//判断当前页数是否越界
private function checkPage(){
if($this->page>$this->maxPage){
$this->page = $this->maxPage;
}
if($this->page<1){
$this->page=1;
}
}
//封装url上搜索条件
private function getParams(){
foreach($_GET as $k=>$v){
if($k!="p" && !empty($v)){
$this->params.="&{$k}={$v}";
}
}
}
//生成分页HTML代码
public function show(){
$str = "";
$str.= "当前第{$this->page}/{$this->maxPage}页 共计{$this->maxRows}条 ";
$str.= " <a href='{$this->url}?p=1{$this->params}'>首页</a> ";
$str.= " <a href='{$this->url}?p=".($this->page-1)."{$this->params}'>上一页</a> ";
$str.= " <a href='{$this->url}?p=".($this->page+1)."{$this->params}'>下一页</a> ";
$str.= " <a href='{$this->url}?p={$this->maxPage}{$this->params}'>末页</a> ";
return $str;
}
}