php原生分页类

<?php
class wpqPageModel {
    public $error = '';

    public $currentPageField; //获取当前页的参数名
    public $currentPage;//当前页存储变量
    public $totalRecord;//总记录数
    public $maxPage;//分页最大页码
    public $pageSize ;//每页显示多少条

    public $pageOffset = 3;//当前分页单侧显示多少页码
    public $leftPage ;//当前分页最小页码
    public $rightPage ;//当前分页最大页码

    public $limit ;//mysql sql语句 的limit 部分 *LIMIT 2,20

    public function __construct($totalRecord = '', $currentPageField = 'p', $mode = 'auto'){
        $this->totalRecord = $totalRecord;
        $this->currentPageField = $currentPageField;
        if($mode == 'auto'){
            $this->run();
        }
    }

    public function run(){
        if(empty($this->totalRecord) || !is_numeric($this->totalRecord)){
            $this->totalRecord = 0;
        }
        if(empty($this->pageSize)){
            $this->pageSize = 50;
        }
        if(empty($this->maxPage)){
            $this->maxPage = ceil( $this->totalRecord * 1.0/$this->pageSize);
        }
        if(empty($this->currentPage)){
            if(!isset($_REQUEST[$this->currentPageField]) || empty($_REQUEST[$this->currentPageField])){
                $this->currentPage = 1;
            }else{
                $this->currentPage = $_REQUEST[$this->currentPageField];
            }
            $this->currentPage = $this->currentPage > $this->maxPage &&  $this->maxPage > 0 ? $this->maxPage : $this->currentPage;
        }
        if(empty($this->pageOffset)){
            $this->pageOffset = 3;
        }
        if(empty($this->leftPage) || empty($this->rightPage) ){
            $this->setOffsetInfo();
        }
    }

    public function getLimit(){
        $offest = $this->pageSize * ( $this->currentPage - 1 );
        $limit = " LIMIT  $offest, $this->pageSize ";
        return $limit;
    }

    public function setOffsetInfo(){
        if($this->currentPage <= $this->pageOffset){
            $this->leftPage = 1;
            $this->rightPage = min(2 * $this->pageOffset +1,  $this->maxPage );
        }
        if( $this->currentPage > $this->pageOffset && $this->currentPage <= $this->maxPage - $this->pageOffset){
            $this->leftPage = $this->currentPage - $this->pageOffset;
            $this->rightPage = $this->currentPage + $this->pageOffset;
        }
        if( $this->currentPage > $this->maxPage - $this->pageOffset){
            $this->leftPage = max($this->currentPage - 2 * $this->pageOffset -1, 1);
            $this->rightPage = $this->maxPage;
        }
    }

    public function getPageInfo(){
        $pageArr = array();
        $pageArr['leftPage'] = $this->leftPage; //分页显示:最左边的页码
        $pageArr['rightPage'] = $this->rightPage; //分页显示:最右边的页码
        $pageArr['currentPage'] = $this->currentPage;//分页显示:当前页码
        $pageArr['totalRecord'] = $this->totalRecord;//分页显示:总记录数
        $pageArr['maxPage'] = $this->maxPage;//分页显示:最大页码
        $pageArr['pageSize'] = $this->pageSize;//分页显示:每页显示多少条
        return $pageArr;
    }

    public function getPageHtml2(){

        $htmlStr = '';
        $htmlStr .= '<script>';
        $htmlStr .= "
        $(document).ready(function(){
              $('.pageTab').click(function(){
                var click_page = $(this).html();
                click_page = click_page == '首' ? 1 :click_page;
                click_page = click_page == '尾' ? '$this->maxPage' : click_page;
    
                var href = location.href;
                var tag = location.href.indexOf('?') > -1 ? '&' : '?';
                if( href.indexOf('&p=') > -1){
                    newhref = href.replace(/&p=\d*/g, tag + 'p='+click_page);
                }else{
                    newhref = href + tag + 'p='+click_page;
                }
                location.href = newhref;
            });
        })
        ";
        $htmlStr .= '</script>';

        $htmlStr .= '<style>';
        $htmlStr .= '.pageline{ float:right;}';
        $htmlStr .= '.pageTab{ width:30px; height: 30px; text-align: center; line-height: 20px; border:1px solid black;';
        $htmlStr .= 'float:left; margin:10px; padding:3px; cursor:pointer;}';
        $htmlStr .= '.pagechoose{ background-color:#2ae;color:white;}';
        $htmlStr .= '</style>';

        $htmlStr .= '<div class="" style="float:left;">';
        $htmlStr .= '总记录数 :' . $this->totalRecord;
        $htmlStr .= '</div>';

        if($this->totalRecord > 0){
            $htmlStr .= '<div class="pageline clearfix">';
            $htmlStr .= '<div class="pageTab">首</div>';
            for($j = $this->leftPage; $j <= $this->rightPage; $j++){
                $htmlStr .= '<div class="pageTab';
                if($j == $this->currentPage){
                    $htmlStr .= ' pagechoose';
                }
                $htmlStr .= '">'. $j .'</div>';
            }
            $htmlStr .= '<div class="pageTab">尾</div>';
            $htmlStr .= ' </div>';
        }

        $htmlStr .= '<div style="clear:both;">';
        $htmlStr .= '</div>';
        return $htmlStr;
    }

    public function getPageHtml(){
        $htmlStr = '';
        $htmlStr .= '<script>';
        $htmlStr .= "
        $(document).ready(function(){
            $('#pageSelect').change(function(){
                 var click_page = $(this).val();
                 var href = location.href;
                 var tag = location.href.indexOf('?') > -1 ? '&' : '?';
                 if( href.indexOf('&p=') > -1){
                    newhref = href.replace(/&p=\d*/g, tag + 'p='+click_page);
                 }else{
                    newhref = href + tag + 'p='+click_page;
                 }
                // location.href = newhref;
                 parentHref(newhref);
            });
            $('.pageTab').click(function(){
                var click_page = '';
                if($(this).find('.fa-chevron-left').size()>0){
                    click_page = $this->currentPage - 1;
                }
                if($(this).find('.fa-chevron-right').size()>0){
                    click_page = $this->currentPage + 1;
                }
                var href = location.href;
                 var tag = location.href.indexOf('?') > -1 ? '&' : '?';
                 if( href.indexOf('&p=') > -1){
                    newhref = href.replace(/&p=\d*/g, tag + 'p='+click_page);
                 }else{
                    newhref = href + tag + 'p='+click_page;
                 }
                 parentHref(newhref);
                 //location.href = newhref;
            });
        })
        ";
        $htmlStr .= '</script>';
        $htmlStr .= '<style>';
        $htmlStr .= '.pageline{float:right;}';
        $htmlStr .= '.pageTab{border:1px solid #eee; margin-left: 5px; margin-right: 5px;  float:left; cursor:pointer;';
        $htmlStr .= ' width:30px; text-align: center; height:30px; line-height: 30px;}';
        $htmlStr .= '.pageTabDisable{border:1px solid #eee; margin-left: 5px; margin-right: 5px; background-color:#1ab394; float:left; ';
        $htmlStr .= ' cursor:not-allowed;width:30px; text-align: center; height:30px; line-height: 30px;}';
        $htmlStr .= '.pageNumber{margin-left: 5px; margin-right: 5px;float:left; text-align: center; height:30px; line-height: 30px;}';
        $htmlStr .= '.pageTabBody{width:100px; float:left;cursor:pointer; height:30px; line-height: 30px;}';
        $htmlStr .= '.pageTabBody select{width:100px; height: 30px;color:#1ab394;border:1px solid #eee; }';
        $htmlStr .= '</style>';

        if($this->totalRecord > 0){
            $htmlStr .= '<div class="pageline clearfix">';
            $htmlStr .= '<div class="pageNumber"><span>共'. $this->totalRecord .'条</span></div>';
            if($this->currentPage > 1){
                $htmlStr .= '<div class="pageTab"><span class="fa fa-chevron-left"></span></div>';
            }else{
                $htmlStr .= '<div class="pageTabDisable"><span class="fa fa-chevron-left"></span></div>';
            }

            $htmlStr .= '<div class="pageTabBody">';
            $htmlStr .= '<select id="pageSelect">';
            for($j = 1; $j <= $this->maxPage; $j++){
                $htmlStr .= '<option value="'. $j .'"';
                if($j == $this->currentPage){
                    $htmlStr .= ' selected';
                }
                $htmlStr .= '>第'. $j .'页</option>';
            }
            $htmlStr .= '</select>';
            $htmlStr .= '</div>';

            if($this->currentPage < $this->maxPage){
                $htmlStr .= '<div class="pageTab"><span class="fa fa-chevron-right"></span></div>';
            }else{
                $htmlStr .= '<div class="pageTabDisable"><span class="fa fa-chevron-right"></span></div>';
            }
            $htmlStr .= ' </div>';
        }

        $htmlStr .= '<div style="clear:both;">';
        $htmlStr .= '</div>';
        return $htmlStr;
    }
}

调用

```php
		//$totalRecord 数据总共有多少条 总记录数
		//p 当前页的请求参数名
        $pageMdl = new wpqPageModel($totalRecord, 'p');
        $returnData['page'] = $pageMdl->getPageInfo();//分页结果数据
 		$returnData['pageHtml'] = $pageMdl->getPageHtml();//分页的参考html
        $limit = $pageMdl->getLimit();//用于sql查询的 limit部分

?>

分页效果 
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210213035927829.png)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值