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 需要得出数据总共有多少条
		//page 当前页的请求参数名
        $pageMdl = new wpqPageModel($totalRecord, 'page');
        $returnData['page'] = $pageMdl->getPageInfo();//分页结果数据
 		$returnData['pageHtml'] = $pageMdl->getPageHtml();//分页的参考html
        $limit = $pageMdl->getLimit();//用于sql查询的 limit部分

?>


上一篇:python爬取post请求Reque Payload的json数据


下一篇:C# 常用公共方法