PHP自定义带省略号的分页类

有时候框架提供的分页类,不能够满足我们的需求,这个时候就需要进行自定义分页类来实现分页了

pageno = ceil($total / $pagesize);
        $this->url = $url;
        $this->maxpageno = $maxpageno < 7 ? 7 : $maxpageno;
    }

    //分页
    function render($curpage = 1, $curclass = 'active')
    {
        if ($pos = strrpos($this->url, '.')) {
            $this->suffix = substr($this->url, $pos);
            $this->url = substr($this->url, 0, $pos);
        }
        //上页下页
        $pre_url = $next_url = 'javascript:;';
        if ($curpage > 1) {
            $pre_url = $this->url . ($curpage - 1) . $this->suffix;
        }
        if ($curpage < $this->pageno) {
            $next_url = $this->url . ($curpage + 1) . $this->suffix;
        }

        //省略
        if ($this->pageno > $this->maxpageno) {
            $half = floor(($this->maxpageno - 4) / 2);
            $half_start = $curpage - $half + 1;
            if ($this->maxpageno % 2 !== 0) --$half_start;
            $half_end = $curpage + $half;
        }
        if (($this->pageno - $curpage) < ($this->maxpageno - 3)) {
            $half_start = $this->pageno - $this->maxpageno + 3;
            unset($half_end);
        }
        if ($curpage <= ($this->maxpageno - 3)) {
            $half_end = $this->maxpageno - 2;
            unset($half_start);
        }
        if ($curpage == 1) {
            $page = $this->getpage($pre_url, ' class="disabled"', '上一页');
        } else {
            $page = $this->getpage($pre_url, '', '上一页');
        }

        for ($i = 1; $i <= $this->pageno; $i++) {
            if (isset($half_start) && $i < $half_start && $i > 1) {
                if ($i == 2) $page .= $this->getpage('javascript:;', ' class="disabled"', '...');
                continue;
            }
            if (isset($half_end) && $i > $half_end && $i < $this->pageno) {
                if ($i == ($half_end + 1)) $page .= $this->getpage('javascript:;', ' class="disabled"', '...');
                continue;
            }

            if ($i == $curpage) {
                $in = " class='{$curclass}'";
                $url = 'javascript:;';
            } else {
                $in = '';
                $url = $this->url . $i . $this->suffix;
            }
            $page .= $this->getpage($url, $in, $i);
        }
        if ($curpage == $this->pageno) {
            $page .= $this->getpage($next_url, ' class="disabled"', '下一页');
        } else {
            $page .= $this->getpage($next_url, '', '下一页');
        }

        return sprintf(
            '
    %s
', $page ); } private function getpage($url, $class, $i) { if ($url == 'javascript:;') { return "{$i}"; } else { return "{$i}"; } } } $curpage = isset($_GET['page']) ? $_GET['page'] : 1; $page = new Pager(35, 2, '?page='); ?>

动态分页:

pagelist($curpage)?>

静态分页,看url:

url = '/html/news/list_.html'; echo $page->pagelist($curpage); ?>

CSS样式

.pagination{text-align: center; margin-top: 35px;}
.pagination a, .pagination span{padding: 3px 10px; background: #e8eaeb; display: inline-block; text-align: center; line-height: 25px; color: #818181; font-size: 14px}
.pagination a:hover, .pagination .fenye-on, .pagination li.active span{background: #0395D5 !important; color: #fff !important}
.pagination li{display: inline-block;margin: 0 5px;}
.pagination li.disabled span{cursor: no-drop; background: #f7f7f7; color: #ccc;}

图片

以上这篇PHP自定义带省略号的分页类就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。

新闻标题:PHP自定义带省略号的分页类
浏览地址:http://www.mswzjz.cn/qtweb/news14/384564.html

攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能