Thinkphp3.2分页实现

1、说明

下文给出的分页类可以在项目中方便的引入,并且可以自定义页码的样式和其他显示的效果。
实现的效果如图所示:
0A$R7IOSU@$63EU[MKBCIHA.png

2、具体实现

1、新建控制器PageController.class.php到项目下

分页源码:

<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 2016-09-22
 * Time: 8:12
 */
namespace Common\Controller;
use Think\Controller;
use Think\Page;

class PageController{
    private $total;          //总得记录条数
    private $pagesize;       //单次查询的记录条数
    private $pagenum;        //总页数
    private $curpage;        //当前页码
    private $limit;          //显示的数字号码最大长度
    private $url;

    //初始化
    function __construct($total,$pagesize,$url,$limit){
        $this->total = $total;
        $this->pagesize = $pagesize;
        $this->pagenum = ceil($this->total / $this->pagesize);
        $this->curpage = $this->setCurPage();
        $this->limit = $limit;
        $this->url = $url;
    }
    //数字目录
    function pageList(){
        $pagelist = '';
        //pagenum = 8 limit = 5
        if($this->pagenum >= $this->limit){
            if ($this->curpage < $this->limit){
                for ($i = 1 ;$i <= $this->limit ; $i++) {
                    if ($this->curpage == $i) {
                        $pagelist .= $this->createStyleSheet("<a href=".'#'."><span class='active'>$i</span></a>",2);
                    } else {
                        $pagelist .= $this->createStyleSheet("<a href=" . $this->setUrl($i) . ">$i</a>");
                    }
                }
            }else{
                if($this->curpage - $this->pagenum  == 0){
                    for ($i = ($this->curpage - $this->limit + 1) ;$i <= $this->curpage ; $i++) {
                        if ($this->curpage == $i) {
                            $pagelist .= $this->createStyleSheet("<a href=".'#'."><span class='active'>$i</span></a>",2);
                        } else {
                            $pagelist .= $this->createStyleSheet("<a href=" . $this->setUrl($i) . ">$i</a>");
                        }
                    }
                }else{
                    //10-5+2
                    //234 5 6
                    //345 6 7
                    //2345 6 7 3456 7 8
                    $pre = 1; //这两个值 是用来控制 分页带出来的页码数量
                    $suff = 2;
                    for ($i = ($this->curpage - $this->limit + 2) + $pre ;$i <= $this->curpage + $suff  ; $i++) {
                        if ($this->curpage == $i) {
                            $pagelist .= $this->createStyleSheet("<a href=".'#'."><span class='active'>$i</span></a>",2);
                            $pagelist .= $this->createStyleSheet("<a href=" . $this->setUrl($i+1) . ">".($i+1)."</a>");
                        } else {
                            $pagelist .= $this->createStyleSheet("<a href=" . $this->setUrl($i) . ">$i</a>");
                        }
                    }
                }
            }
        }else{
            for($i = 1 ; $i <= $this->pagenum ; $i++){
                if($this->curpage == $i){
                    $pagelist .= $this->createStyleSheet("<a href=".'#'."><span class='active'>$i</span></a>",2);
                }else{
                    $pagelist .= $this->createStyleSheet("<a href=" . $this->setUrl($i) . ">$i</a>");
                }
            }
        }
        return $pagelist;
    }
    //获取当前页码
    function setCurPage(){
        $curpage = $_GET['page'];
        if($curpage){
            if($curpage>$this->pagenum) {
                return $this->pagenum;
            }
            else{
                return $curpage;
            }
        }else{
            return 1;
        }
    }
    //URL构造
    function setUrl($curpage = 1){
        //这里使用了TP3.2里面自带的U函数来构造url
        $url =U($this->url,array('page'=>$curpage));
        return $url;
    }
    //首页
    function first(){
        if($this->curpage == 1){
            return  $this->createStyleSheet("<a href=".'#'."><span>首页</span></a>",3);
        }
        return $this->createStyleSheet("<a href=".$this->setUrl()."><span>首页</span></a>");
    }
    //上一页
    function pre(){
        if($this->curpage == 1){
            return  $this->createStyleSheet("<a href=".'#'."><span>上一页</span></a>",3);
        }
        return  $this->createStyleSheet("<a href=".$this->setUrl($this->curpage-1)."><span>上一页</span></a>");
    }
    //下一页
    function next(){
        if($this->curpage == $this->pagenum){
            return  $this->createStyleSheet("<a href=".'#'."><span>下一页</span></a>",3);
        }
        return  $this->createStyleSheet("<a href=".$this->setUrl($this->curpage+1)."><span>下一页</span></a>");
    }
    //后缀
    function setPage(){
        return '共'.$this->total.'条记录|第'.$this->curpage.'页/共'.$this->pagenum.'页';
    }

    //来自定义构建页码的样式
    private function createStyleSheet($item,$index=1){
        $style = '';
        switch ($index){
            //普通页码的样式
            case 1: $style = '<li class="paginate_button" aria-controls="editable" tabindex="0">'.$item.'</li>';break;
            //当前被选中的页码的样式
            case 2: $style = '<li class="paginate_button active" aria-controls="editable" tabindex="0">'.$item.'</li>';break;
            //不可选中页码样式
            case 3: $style = '<li class="paginate_button disabled" aria-controls="editable" tabindex="0">'.$item.'</li>';break;
            default: break;
        }
        return $style;
    }
    //输出分页信息
    function pageinfo(){
        $page = $this->first();    //首页
        $page .= $this->pre();      //上一页
        $page .= $this->pageList(); //数字目录
        $page .= $this->next();     //下一页
//        $page .= $this->setPage();   //后缀
        $out = array(
            'page'=>$page,
            'suff'=>$this->setPage()
        );
        return $out;
    }
}

2、引入分类类,用命名空间的方式来引入use ...,别忘了。

    public function orderManage(){
        $miaomuModel = D('Miaomu');
        $total = $miaomuModel->count(1); //获取所有记录的总数,使用count(1)查询效率更高
        /** @var PageController $page */ 
        $page = new PageController($total,10,'Admin/Order/orderManage',5);
        //参数的意义可以对照PageController.class.php来查看,分别是所有记录的总数、单词查询的数量、要跳转的URL的地址(这里之所有这么因为分页类中使用了U函数来构造url)、页码显示的最大长度
        $res = $miaomuModel->page(I('get.page','1'))->limit(10)->order('id desc')->select();
        //查询单词的记录 这里为10条
        $pageinfo = $page->pageinfo();
        //这就是要在前端显示的分页的数据了
        $this->assign(
            array(
                'res'=>$res,
                'pageinfo'=>$pageinfo['page'],
                'prefix'=>$pageinfo['suff'],
            )
        );
        $this->display('orderManager');
    }

3、前端显示。样式主要使用了jQuery.databasetable.css。下面给出官方的CDN资源地址http://cdn.datatables.net/plug-ins/28e7751dbec/integration/bootstrap/3/dataTables.bootstrap.css

你可以根据前端想要的结果,自己修改分页类源码,来将"首页","下一页","上一页"这些信息输出。
前端的主要分页代码如下:

{$res}
<!--在这里遍历结果集-->
<div class="row">
                        <div class="col-sm-6">
                            <div class="dataTables_info" id="editable_info" role="alert" aria-live="polite" aria-relevant="all">
                                {$prefix}
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
                                <ul class="pagination">
                                    {$pageinfo}
                                </ul>
                            </div>
                        </div>
                    </div>
添加新评论