自己修改的分页类

jerry PHP 2015年11月18日 收藏
自己修改的thinkphp分页类

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> modify:nymz@qq.com
// |
// +----------------------------------------------------------------------
class Page {
// 分页栏每页显示的页数
public $rollPage;
// 页数跳转时要带的参数
public $parameter ;
// 分页URL地址
public $url = '';
// 默认列表每页显示行数
public $listRows =15;
// 起始行数
public $firstRow ;
// 分页总页面数
protected $totalPages ;
// 总行数
protected $totalRows ;
// 当前页数
protected $nowPage ;
// 分页的栏的总页数
protected $coolPages ;
// 分页显示定制
protected $config=array('header'=>'','total'=>'','di'=>'','ye'=>'','theme'=>'<span>%total%%totalRow%%header% %di%%nowPage%/%totalPage%%ye%</span>%first%%upPage%%linkPage%%downPage%%end%');
// 默认分页变量名
protected $varPage;

/**
* 架构函数
* @access public
* @param array $totalRows 总的记录数
* @param array $listRows 每页显示记录数
* @param array $parameter 分页跳转的参数
*/
public function __construct($totalRows,$listRows='',$parameter='',$url='') {
$this->totalRows = $totalRows; //总行数
$this->parameter = $parameter;
$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
$this->rollPage = C('PAGE_ROLLPAGE');
if(!empty($listRows)) {
$this->listRows = intval($listRows);
}
//总页数=总条数/每页条数
$this->totalPages = ceil($this->totalRows/$this->listRows);
//总分页数 =总页数/分页栏每页显示的页数
$this->coolPages = ceil($this->totalPages/$this->rollPage);
$this->nowPage = !empty($_GET[$this->varPage]) ? intval($_GET[$this->varPage]) : 1;
if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
$this->nowPage = $this->totalPages;
}
$this->firstRow = $this->listRows*($this->nowPage-1);
}

public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name]=$value;
}
}
/**
* 分页显示输出
*/
public function show() {
//如果总行数为0时不显示分页;或者总页数小于等于1时不显示分页
if($this->totalRows == 0 OR $this->totalPages <= 1){
return '';
}
$p = $this->varPage;
$nowCoolPage = ceil($this->nowPage/$this->rollPage);

// 分析分页参数
if($this->url){
$depr = C('URL_PATHINFO_DEPR');
$url = rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__';
}else{
if(empty($_GET)) {
$parameter= array();
}else{
$parameter= $_GET;
}
$parameter[$p] = '__PAGE__';
$url = U('',$parameter);
}
//上下翻页字符串
$upRow = $this->nowPage-1;
$downRow = $this->nowPage+1;
if ($upRow>0){
$upPage = "<a href='".str_replace('__PAGE__',$upRow,$url)."'>".L('PREVIOUS')."</a>";
}else{
$upPage = "<span>".L('PREVIOUS')."</span>";
}
//下一页
if ($downRow <= $this->totalPages){
$downPage = "<a href='".str_replace('__PAGE__',$downRow,$url)."'>".L('NEXT')."</a>";
}else{
$downPage = "<span>".L('NEXT')."</span>";
}

//如果当前页等于1时,不可用首页链接;否则首页链接可用;
if($this->nowPage == 1){
$theFirst = "<span>".L('first_page')."</span>";
}else{
$theFirst = "<a href='".str_replace('__PAGE__',1,$url)."' >".L('first_page')."</a>";
}

if($this->nowPage == $this->totalPages){
$nextPage = "";
$theEnd = "<span>".L('LAST_PAGE')."</span>";
}else{
$theEndRow = $this->totalPages;
$theEnd = "<a href='".str_replace('__PAGE__',$theEndRow,$url)."' >".L('LAST_PAGE')."</a>";
}

//定义偏移量;
$linkPage ="";
$offset= ceil($this->rollPage/2-1); //定义偏移量;
//如果(当前页 > 偏移量 ) AND 当前页小于(总页数 -偏移量);
if($this->nowPage > $offset && $this->nowPage < ($this->totalPages - $offset)){ //当前页在页码中间靠右时,保持左边有2个页码
$this->page = $this->nowPage - $offset ; //这个2使当前页保持在中间(每次显示5个页码时),如果一次显示7个页码,改成3即可保持当前页在中间
for($i = 1; $i <= $this->rollPage; $i++){
if($this->page == $this->nowPage){
$linkPage .= "<span class='current'>".$this->page."</span>";
}else{
$linkPage .= "<a href='".str_replace('__PAGE__',$this->page,$url)."'>".$this->page."</a>";
}
$this->page++;
}
}elseif($this->nowPage >= $this->totalPages - $offset){
//如果当前页 > 总页数-偏移量;
for($i = $this->totalPages - $this->rollPage + 1; $i <= $this->totalPages; $i++){
if($i<=0)$i=1;
$this->page = $i;
if($this->page == $this->nowPage){
$linkPage .= "<span class='current'>".$this->page."</span>";
}else{
$linkPage .= "<a href='".str_replace('__PAGE__',$this->page,$url)."'>".$this->page."</a>";
}
}
}
elseif($this->nowPage <= $offset ){
//当前页在为1时,并且当前页在页码中间靠左
if($this->totalPages < $this->rollPage){
for($i=1; $i < $this->rollPage; $i++){
$this->page = $i;
if($this->page == $this->nowPage){
$linkPage .= "<span class='current'>".$this->page."</span>";
}else{
$linkPage .= "<a href='".str_replace('__PAGE__',$this->page,$url)."'>".$this->page."</a>";
}
}
}else{
for($i=1; $i <= $this->rollPage; $i++){
$this->page = $i;
if($this->page == $this->nowPage){
$linkPage .= "<span class='current'>".$this->page."</span>";
}else{
$linkPage .= "<a href='".str_replace('__PAGE__',$this->page,$url)."'>".$this->page."</a>";
}
}
}

}

//模版替换;
$pageStr = str_replace(
array('%header%','%total%','%totalRow%','%di%','%nowPage%','%totalPage%','%ye%','%first%','%upPage%','%linkPage%','%downPage%','%end%'),
array($this->config['header'],$this->config['total'],$this->totalRows,$this->config['di'],$this->nowPage,$this->totalPages,$this->config['ye'],$theFirst,$upPage,$linkPage,$downPage,$theEnd),$this->config['theme']
);
return $pageStr;
}
}
?>

分页样式需要另外写CSS代码,你明白的!