// +----------------------------------------------------------------------
// | 参考 ThinkPHP 分页类改变而来
// +----------------------------------------------------------------------
// | 有两种样式:1、当面页码始终在中间。 2、当点到分页条两边的页码时,自动翻页(默认)。
// +----------------------------------------------------------------------
// | Author: zbseoag
// +----------------------------------------------------------------------
// $Id: page.class.php 2013-05-26 00:08:49
session_start(); //由于要记录用户是向前,还是向后翻页,故启用了session
class Page{
public $page; //当前页
public $nextPage; //上一页
public $prevPage; //下一页
public $pageMax; //最大页
public $startPage; //分页条起码
public $endPage; // 分页条止码
public $style; //样式:$style = mid
public $parameter;//页数跳转时要带的参数
public $varPage;//默认分页变量
public $url = null; //指定url地址
protected $config = array(
'header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'尾页',
'theme' => '%records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%'
);
/**
+----------------------------------------------------------
* 架造函数
+----------------------------------------------------------
* @param $records 总的记录数
* @param $pageSize 每页显示记录数
* @param $pageVal 分页跳转的参数
* @param $parameter 附加参数
+----------------------------------------------------------
*/
function __construct($records, $pageSize, $pageVal = 'p', $parameter = null){
$this->records = $records;
$this->pageMax = ceil($records/$pageSize);
$this->page= max(intval($_GET[$pageVal]), 1); //最小为1
$this->page = min($this->page, $this->pageMax); //最大为末页
$this->nextPage = $this->page + 1;
$this->prevPage = $this->page - 1;
$this->varPage = $pageVal; //默认分页变量名
$this->parameter = $parameter; //附加 url 参数
}
/**
+----------------------------------------------------------
* 设置样式
+----------------------------------------------------------
* @param $name 内置参数名
* @param $value 给定参数值
+----------------------------------------------------------
*/
function setConfig($name, $value){
if(isset($this->config[$name])){
$this->config[$name] = $value;
}
}
/**
+----------------------------------------------------------
* 分页显示输出
+----------------------------------------------------------
* @param $length 分页条长度
+----------------------------------------------------------
* @param $hide 是否适时隐藏上、下一页,比如:当前是第一页,则隐藏上一页
+----------------------------------------------------------
*/
function show($length, $hide = false){
if(0 == $this->records) return '';
empty($this->url)? $this->url = $_SERVER['REQUEST_URI'] : null;
$url = $this->url.(strpos($this->url,'?')?'':'?').$this->parameter;
$parse = parse_url($url);
if(isset($parse['query'])){
parse_str($parse['query'],$params);
unset($params[$this->varPage]);
$url = $parse['path'].'?'.http_build_query($params);
}
if('mid' == $this->style){ //当前页居中
if($length % 2 != 0 ){
$midder = ceil($length / 2);
$big_repair = $midder - 1 ;//当上面以进一法取整,则这里为减1,反之为加1
}else{
$big_repair = $midder = $length / 2;
}
$sml_repair = $midder- 1; //这是小补丁
$this->startPage = ($this->page + $midder) > $this->pageMax ? $this->pageMax - $length : $this->page - $sml_repair;
$this->endPage = ($this->page < $midder)? $length : $this->page + $big_repair;
}else{ //最后一页翻页
$this->startPage = ($this->page >= $length)? $_SESSION['startPage'] : 1;
$this->endPage = $this->startPage + $length - 1;
$this->lastPage = isset($_SESSION['lastPage'])? $_SESSION['lastPage'] : 1; //上次的页码
$_SESSION['lastPage'] = $this->page; //保存当前页
if($this->lastPage < $this->page){ //后翻页
if($this->page >= $this->endPage){
$this->startPage = $_SESSION['startPage'] = $this->page;
$this->endPage = $this->startPage + $length -1;
}
}elseif($this->lastPage == $this->page){//切换
if($this->page == $this->endPage){
$this->startPage = $_SESSION['startPage'] = $this->page;
$this->endPage = $this->startPage + $length -1;
}else{
$this->startPage = $_SESSION['startPage'] = $this->page - $length + 1;
$this->endPage = $this->startPage + $length -1;
}
}else{//前翻页
if($this->page == $this->startPage ){
$this->startPage = $_SESSION['startPage'] = $this->page - $length + 1;
$this->endPage = $this->startPage + $length -1;
}
}
//echo '开始页码:'. $this->startPage.' ';
//echo '上次页码:'. $this->lastPage.' ';
}
//修正一下
$this->startPage = min($this->pageMax - $length + 1, $this->startPage); //最大
$this->startPage = max($this->startPage, 1);//最小为第一页(先求最大,再求最小)
$this->endPage = $this->startPage + $length -1;
$this->endPage = min($this->pageMax, $this->endPage); //最大为末页
$_SESSION['startPage'] = $this->startPage; //修正之后,重新保存
//echo '开始页码:'. $this->startPage.' ';
//echo '上次页码:'. $this->lastPage.' ';
if($this->page <= 1){//如果已经是首页
$firstPage = ''.$this->config['first'].'';
$prevPage = ''.$this->config['prev'].'';
}else{
$firstPage = "".$this->config['first'].'';
$prevPage = "".$this->config['prev'].'';
}
if($this->page >= $this->pageMax){ //如果已经是末页
$nextPage = ''.$this->config['next'].'';
$lastPage = ''.$this->config['last'].'';
}else{
$lastPage = "".$this->config['last'].'';
$nextPage = "".$this->config['next'].'';
}
if($hide){ //如果是隐藏模式
($this->nextPage >= $this->pageMax)? $nextPage = '' : null;
($this->prevPage < 1)? $prevPage = '' : null;
}
for($i = $this->startPage; $i <= $this->endPage; $i++){
if ($this->page == $i)
$linkPage .= "$i";
else
$linkPage .= "$i";
}
$search = array('%records%','%header%','%page%','%pageMax%','%firstPage%','%prevPage%','%linkPage%','%nextPage%','%lastPage%');
$replace = array($this->records,$this->config['header'],$this->page,$this->pageMax,$firstPage,$prevPage,$linkPage,$nextPage,$lastPage);
$pageStr = str_replace($search, $replace, $this->config['theme']);
return $pageStr;
}
}
$page = new Page(340, 10);
$theme = '%records% %header% %page%/%pageMax% 页 %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%';
$page->setConfig('theme',$theme); //可以修改样式,比如:当不要末页是,可以删除 %lastPage%,或者改变位置次序
$page->setConfig('last','末页'); //可自定义文字 : 'header'='条记录','prev'='上一页','next'='下一页','first'='首页','last'='尾页'
$page->url = 'http://localhost/test.php';
echo $page->show(10);
?>
|