php分页类,只需要传入当前页码和数据总条数(样式支持thinkphp5.0)

分页类Page.php

<?php
class Page
{
    private $curPage;//当前页
    private $totalNum;
    private $requestUrl;
    private $params;//分页时带上查询参数
    private $totalPage;
    private $queryString;//查询条件字符串

    public function __construct($curPage = 1, $totalNum = 0, $pageSize = 50, $requestUrl = '', $params = [])
    {
        $this->curPage = $curPage;
        $this->requestUrl = $requestUrl;
        $this->params = $params;
        $this->totalNum = $totalNum;
        $this->totalPage = ceil($totalNum / $pageSize);
        $this->queryString = http_build_query($params);
    }

    public function showPage()
    {
        $html = "<div class='pagination'>";
        $html .= $this->showTotalNum();
        $html .= $this->prePage();
        $html .= $this->pageList();
        $html .= $this->nextPage();
        $html .= "</div>";
        return $html;
    }

    //显示总条数
    private function showTotalNum()
    {
        $html = "<li class='disabled'><span>总条数 {$this->totalNum}</span></li>";
        return $html;
    }

    //上一页
    private function prePage()
    {
        if ($this->curPage == 1) {
            $html = "<li class='disabled'><span>«</span></li>";
        } else {
            $prePage = $this->curPage - 1;
            $html = "<li><a href=\"{$this->requestUrl}?page=$prePage&$this->queryString\">«</a></li>";
        }
        return $html;
    }

    //页码列表
    private function pageList()
    {
        $html = '';
        if ($this->curPage < 7) {
            if ($this->totalPage > 10) {
                $lastPage1 = $this->totalPage;
                $lastPage2 = $this->totalPage - 1;
                for ($i = 1; $i <= 8; $i++) {
                    if ($this->curPage == $i) {
                        $html .= "<li class='active'><span>$i</span></li>";
                    } else {
                        $html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
                    }
                }
                $html .= "<li class='disabled'><span>...</span></li>";
                $html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage1&$this->queryString\">$lastPage2</a></li>";
                $html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage2&$this->queryString\">$lastPage1</a></li>";
            } else {
                for ($i = 1; $i <= $this->totalPage; $i++) {
                    if ($this->curPage == $i) {
                        $html .= "<li class='active'><span>$i</span></li>";
                    } else {
                        $html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
                    }
                }
            }
        } else if ($this->curPage >= 7) {
            $html .= "<li><a href=\"{$this->requestUrl}?page=1&$this->queryString\">1</a></li>";
            $html .= "<li><a href=\"{$this->requestUrl}?page=2&$this->queryString\">2</a></li>";
            $html .= "<li class='disabled'><span>...</span></li>";
            $curPage = $this->curPage;
            $totalPage = $this->totalPage;
            if ($totalPage >= 13 && ($totalPage - $curPage) > 5) {
                for ($i = $curPage - 3; $i <= $curPage + 3; $i++) {
                    if ($i == $curPage) {
                        $html .= "<li class='active'><span>$i</span></li>";
                    } else {
                        $html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
                    }
                }
                $lastPage1 = $totalPage;
                $lastPage2 = $totalPage - 1;
                $html .= "<li class='disabled'><span>...</span></li>";
                $html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage1&$this->queryString\">$lastPage2</a></li>";
                $html .= "<li><a href=\"{$this->requestUrl}?page=$lastPage2&$this->queryString\">$lastPage1</a></li>";
            } else {
                for ($i = $curPage - 3; $i <= $this->totalPage; $i++) {
                    if ($i == $curPage) {
                        $html .= "<li class='active'><span>$i</span></li>";
                    } else {
                        $html .= "<li><a href=\"{$this->requestUrl}?page=$i&$this->queryString\">$i</a></li>";
                    }
                }
            }
        }
        return $html;
    }

    //下一页
    private function nextPage()
    {
        $html = '';
        if ($this->totalPage != 1) {
            $nextPage = $this->curPage + 1;
            $html = "<li><a href=\"{$this->requestUrl}?page=$nextPage&$this->queryString\">»</a></li>";
        }
        return $html;
    }
}

 

控制器中调用

//引入文件
require_once(CMF_ROOT.'simplewind/extend/paginate/Page.php');
//总数据条数
$totalNum = 1000;
//不带参数的请求地址
$requestUrl = '/Admin/pdf_word_user/index.html';
$page = new \Page($pgnum,$totalNum,$pgsize,$requestUrl,['a'=>123,'b'=>456]);
$pageHtml = $page->showPage();

$this->assign("pageHtml", $pageHtml);
return $this->fetch();

 

 

视图层程现样式

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值