Home >php教程 >php手册 >自己修改的分页类

自己修改的分页类

WBOY
WBOYOriginal
2016-06-07 11:43:14909browse

自己修改的thinkphp分页类
自己修改的分页类
// +----------------------------------------------------------------------
// | 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 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'=>'%total%%totalRow%%header% %di%%nowPage%/%totalPage%%ye%%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 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 = "".L('PREVIOUS')."";
}else{
$upPage = "".L('PREVIOUS')."";
}
//下一页
if ($downRow totalPages){
$downPage = "".L('NEXT')."";
}else{
$downPage = "".L('NEXT')."";
}

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

if($this->nowPage == $this->totalPages){
$nextPage = "";
$theEnd = "".L('LAST_PAGE')."";
}else{
$theEndRow = $this->totalPages;
$theEnd = "".L('LAST_PAGE')."";
}

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

}

//模版替换;
$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代码,你明白的!

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn