一个简单且很好用的php分页类


Posted in PHP onOctober 26, 2013
class Page {
    // 分页栏每页显示的页数
    public $rollPage = 6;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array(
     'redirect'=>false,
     'header'=>'条记录',
     'prev'=>'上一页',
     'next'=>'下一页',
     'first'=>'1',
     'last'=>'最后一页',
     'theme'=>' <div class="part1">%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage%</div> <div class="part2">共  %totalPage% 页');
    // 默认分页变量名
    protected $varPage;
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$parameter='') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        //$_GET验证
        $this->nowPage = intval($_GET[$this->varPage]);
        $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 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;
        }
    }
    /**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {
     if(0 == $this->totalRows) return '';
     //处理参数
        $p = $this->varPage;
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        /* 分页逻辑  */       
        //当总数小于显示的页码数
        if ($this->totalPages <= $this->rollPage) {
         $start = 1;
         $end   = $this->totalPages;
        }
        else{
         //
         if  ($this->nowPage <= $this->rollPage - 1) {
          $start = 1;
          $end   = $this->rollPage;
          $islast = true;
         }  
         else if ($this->nowPage > $this->totalPages - $this->rollPage + 1) {
          $start = $this->totalPages - ($this->rollPage - 1);
          $end   = $this->totalPages;
          $isfirst = true;
         }
         else{
          //浮动数
          $size = floor($this->rollPage / 2);
          $start = $this->nowPage - $size;
          $end   = $this->nowPage + $size;
          $isfirst = true;
          $islast = true;
         }
        }
        //上下翻页字符串
        $upRow   = $this->nowPage - 1;
        $downRow = $this->nowPage + 1;
        
        /* 拼装HTML */
        //< 1...     ...last >
        if ($isfirst){
         $theFirst = "<a class='firstPage' href='".$url."&".$p."=1' >".$this->config['first']."</a>";
        }
        if ($islast){
         $theEnd = "<a class='lastPage' href='".$url."&".$p."=$this->totalPages' >".$this->config['last']."</a>";
        }
        if ($upRow > 0){
         $upPage = "<a class='upPage' href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
        }
        if ($downRow <= $this->totalPages){
         $downPage = "<a class='downPage' href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
        }
       if($start==3){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a>";
        }
        if($start>=4){
         $linkPage .= "<a href='".$url."&".$p."=2'>2</a> <span class='noEndClass'>...</span>";
        }
        //1 2 3 4 5
        for($i=$start;$i<=$end;$i++){
         if($i!=$this->nowPage){
          $linkPage .= " <a href='".$url."&".$p."=$i'> ".$i." </a>";
         }else{
          $linkPage .= " <span class='current'>".$i."</span>";
         }
         if($i==$end){
          if($i<$this->totalRows){
           $linkPage .= " <span class='noEndClass'>...</span>";
          }
         }
        }
        $pageStr = str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage,$downPage,$theEnd),$this->config['theme']);
        //显示模式  普通false 带跳转ture
        if (!empty($this->config['redirect'])){
         $html = $pageStr;
        }else{
         //传递参数
         if($this->totalPages > 1){
          $redirect = " 到第 <form method='get' action=''><input name=".$p." type='text' class='page_text' size='3' maxlength='3' value='" . $this->nowPage ."'/> 页 <input type='submit' class='page_btn' value='确定' />";
          if ($params){
           foreach($params as $k => $v){
            $string .= "<input type='hidden' name='" . $k . "' value='" . $v . "'/>";
           }
           $redirect = $redirect . $string . '</form></div>';
          }else{
           $redirect = $redirect . '</form></div>';
          }
         }
         //生成Html字符串
         $html = $pageStr . $redirect;       
        }  
        return $html;
    }
}
PHP 相关文章推荐
在任意字符集下正常显示网页的方法二(续)
Apr 01 PHP
php session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
Nov 27 PHP
PHP中冒号、endif、endwhile、endfor使用介绍
Apr 28 PHP
浅析ThinkPHP的模板输出功能
Jul 01 PHP
PHP中使用asort进行中文排序失效的问题处理
Aug 18 PHP
typecho插件编写教程(三):保存配置
May 28 PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
Jan 14 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
Dec 15 PHP
PHP 记录访客的浏览信息方法
Jan 29 PHP
微信JSSDK分享功能图文实例详解
Apr 08 PHP
PHP-FPM 设置多pool及配置文件重写操作示例
Oct 02 PHP
详解Laravel制作API接口
May 31 PHP
PHP中VC6、VC9、TS、NTS版本的区别与用法详解
Oct 26 #PHP
zend optimizer在wamp的基础上安装图文教程
Oct 26 #PHP
php批量更改数据库表前缀实现方法
Oct 26 #PHP
PHP连接MySQL查询结果中文显示乱码解决方法
Oct 25 #PHP
学习php过程中的一些注意点的总结
Oct 25 #PHP
php模拟ping命令(php exec函数的使用方法)
Oct 25 #PHP
php导入导出excel实例
Oct 25 #PHP
You might like
叶罗丽:为什么大家对颜冰这对CP非常关心,却对金茉两人十分冷漠
2020/03/17 国漫
php中批量替换文件名的实现代码
2011/07/20 PHP
用PHP读取超大文件的实例代码
2012/04/01 PHP
解析Ubuntu下crontab命令的用法
2013/06/24 PHP
php加速器eAccelerator的配置参数、API详解
2014/05/05 PHP
php利用cookies实现购物车的方法
2014/12/10 PHP
PHP下SSL加密解密、验证、签名方法(很简单)
2020/06/28 PHP
php框架CodeIgniter使用redis的方法分析
2018/04/13 PHP
关于Javascript作用域链的八点总结
2013/12/06 Javascript
贴近用户体验的Jquery日期、时间选择插件
2015/08/19 Javascript
JavaScript操作 url 中 search 部分方法函数
2016/06/15 Javascript
javascript数组拍平方法总结
2018/01/20 Javascript
webpack将js打包后的map文件详解
2018/02/22 Javascript
Vue实现日历小插件
2019/06/26 Javascript
详解vue-flickity的fullScreen功能实现
2020/04/07 Javascript
小程序实现列表展开收起效果
2020/07/29 Javascript
在Uni中使用Vue的EventBus总线机制操作
2020/07/31 Javascript
jQuery实现简单QQ聊天框
2020/08/27 jQuery
Python中用Spark模块的使用教程
2015/04/13 Python
Python如何实现MySQL实例初始化详解
2017/11/06 Python
Python Numpy计算各类距离的方法
2019/07/05 Python
Python 多个图同时在不同窗口显示的实现方法
2019/07/07 Python
基于Django signals 信号作用及用法详解
2020/03/28 Python
解决pyPdf和pyPdf2在合并pdf时出现异常的问题
2020/04/03 Python
Python爬取微信小程序Charles实现过程图解
2020/09/29 Python
Python列表嵌套常见坑点及解决方案
2020/09/30 Python
HTML5之HTML元素扩展(上)—新增加的元素及使用概述
2013/01/31 HTML / CSS
Lookfantastic葡萄牙官方网站:欧洲第一大化妆品零售商
2018/03/17 全球购物
Amcal中文官网:澳洲综合性连锁药房
2019/03/28 全球购物
企业管理部经理岗位职责
2013/12/24 职场文书
2016元旦主持人经典开场白台词
2015/12/03 职场文书
初中化学教学反思
2016/02/22 职场文书
python opencv旋转图片的使用方法
2021/06/04 Python
JavaScript实现登录窗体
2021/06/22 Javascript
springboot集成flyway自动创表的详细配置
2021/06/26 Java/Android
关于的python五子棋的算法
2022/05/02 Python