Home > php教程 > php手册 > body text

thinkphp分页类改造

WBOY
Release: 2016-06-07 11:44:22
Original
1032 people have browsed it

thinkphp自带的分页,没法通过配置让当前页页码显示在页码条中间(我的还是2.0,不知道3.0+可以了么),所以就改了下,效果还不错
thinkphp分页类改造
thinkphp分页类改造<?php <br /> // +----------------------------------------------------------------------<br> // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]<br> // +----------------------------------------------------------------------<br> // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.<br> // +----------------------------------------------------------------------<br> // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br> // +----------------------------------------------------------------------<br> // | Author: liu21st <liu21st> modified by 米国村长<br> // +----------------------------------------------------------------------<br> // $Id$<br> <br> class Page extends Think {<br>     // 起始行数<br>     public $firstRow    ;<br>     // 列表每页显示行数<br>     public $listRows    ;<br>     // 页数跳转时要带的参数<br>     public $parameter  ;<br>     // 分页总页面数<br>     protected $totalPages  ;<br>     // 总行数<br>     protected $totalRows  ;<br>     // 当前页数<br>     protected $nowPage    ;<br>     // 分页的栏的总页数<br>     protected $coolPages   ;<br>     // 分页栏每页显示的页数<br>     protected $rollPage   ;<br>     //这里将$page写成类的成员属性,其实不用,懒的改了<br>     public $page;<br>     // 分页显示定制<br>     //protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');<br>     protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>'<span>共 %totalRow% %header%</span>   %upPage%  %linkPage% %downPage%');<br> <br>     /**<br>      +----------------------------------------------------------<br>      * 架构函数<br>      +----------------------------------------------------------<br>      * @access public<br>      +----------------------------------------------------------<br>      * @param array $totalRows  总的记录数<br>      * @param array $listRows  每页显示记录数<br>      * @param array $parameter  分页跳转的参数<br>      +----------------------------------------------------------<br>      */<br>     public function __construct($totalRows,$listRows,$parameter='') {<br>         $this->totalRows = $totalRows;<br>         $this->parameter = $parameter;<br>         $this->rollPage = C('PAGE_ROLLPAGE');<br>         $this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');<br>         $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数<br>         $this->coolPages  = ceil($this->totalPages/$this->rollPage);<br>         $this->nowPage  = !empty($_GET[C('VAR_PAGE')]) ? $_GET[C('VAR_PAGE')]:1;<br>         if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {<br>             $this->nowPage = $this->totalPages;<br>         }<br>         $this->firstRow = $this->listRows*($this->nowPage-1);<br>     }<br> <br>     public function setConfig($name,$value) {<br>         if(isset($this->config[$name])) {<br>             $this->config[$name]    =   $value;<br>         }<br>     }<br> <br>     /**<br>      +----------------------------------------------------------<br>      * 分页显示输出<br>      +----------------------------------------------------------<br>      * @access public<br>      +----------------------------------------------------------<br>      */<br>     public function show() {<br>         if(0 == $this->totalRows) return '';<br>         $p = C('VAR_PAGE');<br>         $nowCoolPage      = ceil($this->nowPage/$this->rollPage);<br>         $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;<br>         $parse = parse_url($url);<br>         if(isset($parse['query'])) {<br>             parse_str($parse['query'],$params);<br>             unset($params[$p]);<br>             $url   =  $parse['path'].'?'.http_build_query($params);<br>         }<br>         //上下翻页字符串<br>         $upRow   = $this->nowPage-1;<br>         $downRow = $this->nowPage+1;<br>         if ($upRow>0){<br>             $upPage="<a>".$this->config['prev']."</a>";<br>         }else{<br>             $upPage="<span>上一页</span>";<br>         }<br> <br>         if ($downRow totalPages){<br>             $downPage="<a>".$this->config['next']."</a>";<br>         }else{<br>             $downPage="<span>下一页 </span>";<br>         }<br>         //  >>    //<br>         /*if($nowCoolPage == 1){<br>             $theFirst = "";<br>             $prePage = "";<br>         }else{<br>             $preRow =  $this->nowPage-$this->rollPage;<br>             $prePage = "<a>上".$this->rollPage."页</a>";<br>             $theFirst = "<a>".$this->config['first']."</a>";<br>         }<br>         if($nowCoolPage == $this->coolPages){<br>             $nextPage = "";<br>             $theEnd="";<br>         }else{<br>             $nextRow = $this->nowPage+$this->rollPage;<br>             $theEndRow = $this->totalPages;<br>             $nextPage = "<a>下".$this->rollPage."页</a>";<br>             $theEnd = "<a>".$this->config['last']."</a>";<br>         }<br>      */<br>     // 1 2 3 4 5<br>     <br>     $linkPage = "";<br>     if($totalpage rollPage){<br>         for($i = 1;$itotalPages;$i++){        //若页码数不够设置的显示页数 ,比如设置显示5页,但只有3页数据<br>             if($i == $this->nowPage){<br>                 $linkPage .= "<span>".$i."</span>";<br>             }else{<br>                 $linkPage .= "<a>".$i."</a>";<br>             }<br>         }<br>     }else{<br>         $mid = ceil($this->rollPage/2);<br>         if($this->nowPage >= $mid && $this->nowPage totalPages - $mid)){    //当前页在页码中间靠右时,保持左边有2个页码<br>             $this->page = $this->nowPage - ($this->rollpage/2 - 1);//这个2使当前页保持在中间(每次显示5个页码时),如果一次显示7个页码,改成3即可保持当前页在中间<br>             for($i = 1; $i rollPage; $i++){<br>                 if($this->page == $this->nowPage){<br>                     $linkPage .= "<span>".$this->page."</span>";<br>                 }else{<br>                     $linkPage .= "<a>page'>".$this->page."</a>";<br>                 }<br>                 $this->page++;<br>             }<br>         }elseif($this->nowPage              for($i = 1; $i rollPage; $i++){                //如1234567 当前页为3<br>                 $this->page = $i;<br>                 if($this->page == $this->nowPage){<br>                     $linkPage .= "<span>".$this->page."</span>";<br>                 }else{<br>                     $linkPage .= "<a>page'>".$this->page."</a>";<br>                 }<br>             }<br>         }elseif($this->nowPage > $this->totalPages - $mid){                //当前页在coolPages是最后一页时,直接循环出剩下的页面就行<br>             for($i = $this->totalPages - $this->rollPage + 1; $i totalPages; $i++){<br>                 $this->page = $i;<br>                 if($this->page == $this->nowPage){<br>                     $linkPage .= "<span>".$this->page."</span>";<br>                 }else{<br>                     $linkPage .= "<a>page'>".$this->page."</a>";<br>                 }<br>             }<br> <br>         }<br>     }<br>     <br>         $pageStr     =     str_replace(<br>             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),<br>             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);<br>         return $pageStr;<br>     }<br> <br> }<br> ?></liu21st>

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template