thinkphp自定义分页类

<?php
namespace lib;
class Paginator {
    //总记录
    private $total;
    //每页显示多少条
    private $limit;
    //当前页码
    private $page;
    //总页码
    private $pcount;
    //地址
    private $url;
    //两边保持数字分页的
    private $both_num;
    /**
     * 构造
     */
    public function __construct($total, $limit) {
        $this->total = $total ? $total : 0;
        $this->limit = $limit;
        $this->pcount = ceil($this->total / $this->limit);
        $this->page = $this->setPage();
        $this->url = $this->setUrl();
        $this->both_num = 2;
        if ($this->page >= $this->pcount) {
            $this->page = $this->pcount;
        }
    }
	/*
    * 分页信息
    */
    public function showPage() {
        $page = $this->prev();
        $page .= $this->first();
        $page .= $this->pageList();
        $page .= $this->last();
        $page .= $this->next();
        return $page;
    }
    /*
    * 获取当前页码
    */
    private function setPage() {
        if (isset($_POST['page'])) {
            if ($_POST['page'] > 0) {
                if ($_POST['page'] > $this->pcount) {
                    return $this->pcount;
                } else {
                    return $_POST['page'];
                }
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }
    /*
    * 获取地址
    */
    private function setUrl() {
        $url = $_SERVER['REQUEST_URI'];
        $par = parse_url($url);
        if (isset($par['query'])) {
            parse_str($par['query'], $query);
            unset($query['page']);
            if ($query) {
                $url = $par['path'] . '?' . http_build_query($query) . '&';
            } else {
                $url = $par['path'] . '?';
            }
        } else {
            $url = $par['path'] . '?';
        }
        return $url;
    }
    /*
    * 数字页码
    */
    private function pageList() {
        $plist = '';
        for ($i = $this->both_num; $i >= 1; $i--) {
            $page = $this->page - $i;
            if ($page < 1) continue;
            $plist.= '<a data-page-num="' . $page . '" href="javascript:;">' . $page . '</a>';
        }
        $plist.= '<a href="javascript:;" class="cur">' . $this->page . '</a>';
        for ($i = 1; $i <= $this->both_num; $i++) {
            $page = $this->page + $i;
            if ($page > $this->pcount) break;

            $plist .= '<a data-page-num="' . $page . '" href="javascript:;">' . $page . '</a>';
        }
        return $plist;
    }
    /*
    * 首页
    */
    private function first() {
        if ($this->page > $this->both_num + 1) {
            return '<a data-page-num="1" href="javascript:;">1</a><a href="javascript:;">…</a>';
        }
    }
    /*
    * 上一页
    */
    private function prev() {
        if ($this->page == 1) {
            return '';
        }
        return '<a data-page-num="' . ($this->page - 1) . '" href="javascript:;">上一页</a>';
    }
    /*
    * 下一页
    */
    private function next() {
        if ($this->page == $this->pcount) {
            return '';
        }
        return '<a data-page-num="' . ($this->page + 1) . '" href="javascript:;">下一页</a>';
    }
    /*
    * 尾页
    */
    private function last() {
        if ($this->pcount - $this->page > $this->both_num) {
            return '<a href="javascript:;">…</a><a data-page-num="' . $this->pcount . '" href="javascript:;">' . $this->pcount . '</a>';
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xmode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值