微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

thinkphp简洁、美观、靠谱的分页类

@H_404_0@废话不多说先上图预览下,即本博客分页

@H_404_0@

@H_404_0@这个分页类是在thinkPHP框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;

@H_404_0@1:只有一页内容不显示分页

@H_404_0@2:原分页类在当前页是第一页和最后一页的时候,不显示第一页和最后一页的按钮;

@H_404_0@

@H_404_0@3:分页数比较少时不显示首页和末页按钮;

@H_404_0@4:包裹分页内容的父级div没有class;

@H_404_0@针对以上问题逐一进行了修改成如下;

@H_404_0@1:如果没有数据不显示分页,如果有一页及以上内容显示分页

@H_404_0@2:认就显示第一页和最后一页按钮,但是在当前页是第一页和最后一页的时候按钮点击无效果

@H_404_0@3:认就显示首页和末页按钮;

@H_404_0@4:为包裹分页内容的父级div添加名为page的class;

@H_404_0@5:显示总共查出的内容条数;

@H_404_0@示例环境:thinkPHP3.2.3

@H_404_0@分页类目录:/ThinkPHP/Library/Org/Bjy/Page.class.PHP

@H_404_0@分页代码如下:

@H_404_0@<?PHP

@H_404_0@// +----------------------------------------------------------------------

@H_404_0@// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]

@H_404_0@// +----------------------------------------------------------------------

@H_404_0@// | copyright (c) 2006-2014 http://thinkPHP.cn All rights reserved.

@H_404_0@// +----------------------------------------------------------------------

@H_404_0@// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

@H_404_0@// +----------------------------------------------------------------------

@H_404_0@// | Author: 麦当苗儿 <[email protected]> <http://www.zjzit.cn>

@H_404_0@// +----------------------------------------------------------------------

@H_404_0@/*

@H_404_0@ * PHP分页

@H_404_0@ * 修改者:白俊遥

@H_404_0@ * 日  期:2015.5.10

@H_404_0@ * 邮  箱:[email protected]

@H_404_0@ * 博  客:http://www.baijunyao.com

@H_404_0@ */

@H_404_0@namespace OrgBjy;

@H_404_0@ 

@H_404_0@class Page{

@H_404_0@    public $firstRow; // 起始行数

@H_404_0@    public $listRows; // 列表每页显示行数

@H_404_0@    public $parameter; // 分页跳转时要带的参数

@H_404_0@    public $totalRows; // 总行数

@H_404_0@    public $totalPages; // 分页页面

@H_404_0@    public $rollPage   = 5;// 分页栏每页显示的页数

@H_404_0@    public $lastSuffix = true; // 最后一页是否显示总页数

@H_404_0@ 

@H_404_0@    private $p       = 'p'; //分页参数名

@H_404_0@    private $url     = ''; //当前链接URL

@H_404_0@    private $NowPage = 1;

@H_404_0@ 

@H_404_0@    // 分页显示定制

@H_404_0@    private $config  = array(

@H_404_0@        'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',

@H_404_0@        'first'   => '首页',

@H_404_0@        'prev'   => '上一页',

@H_404_0@        'next'   => '下一页',

@H_404_0@        'last'   => '末页',

@H_404_0@        'theme'  => '%FirsT% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',

@H_404_0@    );

@H_404_0@ 

@H_404_0@    /**

@H_404_0@     * 架构函数

@H_404_0@     * @param array $totalRows  总的记录数

@H_404_0@     * @param array $listRows  每页显示记录数

@H_404_0@     * @param array $parameter  分页跳转的参数

@H_404_0@     */

@H_404_0@    public function __construct($totalRows,$listRows=20,$parameter = array()) {

@H_404_0@        C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称

@H_404_0@        /* 基础设置 */

@H_404_0@        $this->totalRows  = $totalRows; //设置总记录数

@H_404_0@        $this->listRows   = $listRows;  //设置每页显示行数

@H_404_0@        $this->parameter  = empty($parameter) ? $_GET : $parameter;

@H_404_0@        $this->NowPage    = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);

@H_404_0@        $this->NowPage    = $this->NowPage>0 ? $this->NowPage : 1;

@H_404_0@        $this->firstRow   = $this->listRows * ($this->NowPage - 1);

@H_404_0@    }

@H_404_0@ 

@H_404_0@    /**

@H_404_0@     * 定制分页链接设置

@H_404_0@     * @param string $name  设置名称

@H_404_0@     * @param string $value 设置值

@H_404_0@     */

@H_404_0@    public function setConfig($name,$value) {

@H_404_0@        if(isset($this->config[$name])) {

@H_404_0@            $this->config[$name] = $value;

@H_404_0@        }

@H_404_0@    }

@H_404_0@ 

@H_404_0@    /**

@H_404_0@     * 生成链接URL

@H_404_0@     * @param  integer $page 页码

@H_404_0@     * @return string

@H_404_0@     */

@H_404_0@    private function url($page){

@H_404_0@        return str_replace(urlencode('[PAGE]'),$page,$this->url);

@H_404_0@    }

@H_404_0@ 

@H_404_0@    /**

@H_404_0@     * 组装分页链接

@H_404_0@     * @return string

@H_404_0@     */

@H_404_0@    public function show() {

@H_404_0@        if(0 == $this->totalRows) return '';

@H_404_0@ 

@H_404_0@        /* 生成URL */

@H_404_0@        $this->parameter[$this->p] = '[PAGE]';

@H_404_0@        $this->url = U(ACTION_NAME,$this->parameter);

@H_404_0@        /* 计算分页信息 */

@H_404_0@        $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数

@H_404_0@        if(!empty($this->totalPages) && $this->NowPage > $this->totalPages) {

@H_404_0@            $this->NowPage = $this->totalPages;

@H_404_0@        }

@H_404_0@ 

@H_404_0@        /* 计算分页零时变量 */

@H_404_0@        $Now_cool_page      = $this->rollPage/2;

@H_404_0@        $Now_cool_page_ceil = ceil($Now_cool_page);

@H_404_0@ 

@H_404_0@        //上一页

@H_404_0@        $up_row  = $this->NowPage - 1;

@H_404_0@        $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>' : '<a class="prev" href="javascript:;">' . $this->config['prev'] . '</a>';

@H_404_0@ 

@H_404_0@        //下一页

@H_404_0@        $down_row  = $this->NowPage + 1;

@H_404_0@        $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>' : '<a class="next" href="javascript:;">' . $this->config['next'] . '</a>';

@H_404_0@ 

@H_404_0@        //第一页

@H_404_0@        $the_first = '<a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a>';

@H_404_0@ 

@H_404_0@        //最后一页

@H_404_0@        $the_end = '<a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>';

@H_404_0@ 

@H_404_0@        //数字连接

@H_404_0@        $link_page = "";

@H_404_0@        for($i = 1; $i <= $this->rollPage; $i++){

@H_404_0@            if(($this->NowPage - $Now_cool_page) <= 0 ){

@H_404_0@                $page = $i;

@H_404_0@            }elseif(($this->NowPage + $Now_cool_page - 1) >= $this->totalPages){

@H_404_0@                $page = $this->totalPages - $this->rollPage + $i;

@H_404_0@            }else{

@H_404_0@                $page = $this->NowPage - $Now_cool_page_ceil + $i;

@H_404_0@            }

@H_404_0@            if ($page>0) {

@H_404_0@                if($page != $this->NowPage){

@H_404_0@                    if($page <= $this->totalPages){

@H_404_0@                        $link_page .= '<a class="num" href="' . $this->url($page) . '">' . $page . '</a>';

@H_404_0@                    }else{

@H_404_0@                        break;

@H_404_0@                    }

@H_404_0@                }else{

@H_404_0@                    $link_page .= '<span class="current">' . $page . '</span>';

@H_404_0@                }                

@H_404_0@            }

@H_404_0@ 

@H_404_0@        }

@H_404_0@ 

@H_404_0@        //替换分页内容

@H_404_0@        $page_str = str_replace(

@H_404_0@            array('%HEADER%','%Now_PAGE%','%UP_PAGE%','%DOWN_PAGE%','%FirsT%','%LINK_PAGE%','%END%','%TOTAL_ROW%','%TOTAL_PAGE%'),

@H_404_0@            array($this->config['header'],$this->NowPage,$up_page,$down_page,$the_first,$link_page,$the_end,$this->totalRows,$this->totalPages),

@H_404_0@            $this->config['theme']);

@H_404_0@        return '<div class="page">'.$page_str.'</div>';

@H_404_0@    }

@H_404_0@}

@H_404_0@分页调用

@H_404_0@    $count=$this->where($where)->count();

@H_404_0@    $page=new OrgBjyPage($count,$limit);

@H_404_0@    $list=$this->where($where)->order('addtime desc')->limit($page->firstRow.','.$page->listRows)->select();

@H_404_0@    $show=$page->show();

@H_404_0@分页类css:

@H_404[email protected] {

@H_404_0@    background:#fff;

@H_404_0@    Box-shadow:0 1px 2px 0 #e2e2e2

@H_404_0@}

@H_404[email protected] .page {

@H_404_0@    width:100%;

@H_404_0@    padding:30px 15px;

@H_404_0@    background:#fff;

@H_404_0@    text-align:center;

@H_404_0@    overflow:hidden

@H_404_0@}

@H_404[email protected] .page .first,.b-page .page .prev,.b-page .page .current,.b-page .page .num,.b-page .page .next,.b-page .page .end {

@H_404_0@    padding:8px 16px;

@H_404_0@    margin:0 5px;

@H_404_0@    display:inline-block;

@H_404_0@    color:#008cba;

@H_404_0@    border:1px solid #f2f2f2;

@H_404_0@    border-radius:5px

@H_404_0@}

@H_404[email protected] .page .first:hover,.b-page .page .prev:hover,.b-page .page .current:hover,.b-page .page .num:hover,.b-page .page .next:hover,.b-page .page .end:hover {

@H_404_0@    text-decoration:none;

@H_404_0@    background:#f8f5f5

@H_404_0@}

@H_404[email protected] .page .current {

@H_404_0@    background-color:#008cba;

@H_404_0@    color:#fff;

@H_404_0@    border-radius:5px;

@H_404_0@    border:1px solid #008cba

@H_404_0@}

@H_404[email protected] .page .current:hover {

@H_404_0@    text-decoration:none;

@H_404_0@    background:#008cba

@H_404_0@}

@H_404_0@分页类的使用方法和原thinkPHP相同,具体参考:thinkPHP手册-数据分页

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐