php分页思路

2018-06-22 05:35:47来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

<?php
class page{
    public $nowPage=1;
    public $perPage=10;
    public $showPage=10;
    public $totalPage;
    private $startPage=1;
    private $endPage;
    private $pageNum;
    private $url;
    private $query;
    private $html;
    private $defaultConfig=[
        'prev'=>false,
        'next'=>false,
        'total'=>false,
        'first'=>false,
        'last'=>false
    ];
    private $config;
    public function __construct($config='',$url=''){
        $this->config=$config?$config:$this->defaultConfig;
        $this->url=$url?$url:$_SERVER['PHP_SELF'];
    }
    public function show($nowPage,$perPage,$total){
        $this->perPage=$perPage;
        $this->totalPage=intval(ceil($total/$perPage));
        $this->nowPage=$nowPage>$this->totalPage?$this->totalPage:$nowPage;
        $this->initConfig();
        $this->createPageNum();
        $this->createHtml();
        return $this->html;
    }
    private function createPageNum(){
        $half=intval($this->showPage/2);
        $this->startPage=max(1,$this->nowPage-$half);
        $this->endPage=min($this->startPage+$this->showPage-1,$this->totalPage);
        $this->startPage=max(1,$this->endPage-$this->showPage+1);
        $this->pageNum=range($this->startPage, $this->endPage);
    }
    private function createUrl($page){
        $urlArr=$_SERVER['QUERY_STRING'];
        parse_str($urlArr,$queryArr);
        $queryArr['page']=$page<=1?1:$page;
        $queryArr['page']=$queryArr['page']>=$this->totalPage?$this->totalPage:$queryArr['page'];
        return $this->query=http_build_query($queryArr);
    }
    private function initConfig(){
        $configKey=array_keys($this->defaultConfig);
        foreach ($this->config as $k => $v) 
        {
            if(!in_array($k, $configKey)) unset($this->config[$k]);
        }
    }
    private function createHtml(){
        $pageNum=$this->pageNum;
        $html='';
        foreach ($pageNum as $v){
            if($v==$this->nowPage){
                $html.="<span style='margin:10px;display:inline-block;min-width:36px;text-align:center;line-height:36px;'>{$this->nowPage}</span>";
            }else{
                $query=$this->createUrl($v);
                $url=$this->url.'?'.$query;
                $html.="<a style='border:1px solid #e1e2e3;color:blue;text-decoration:none;margin:10px;display:inline-block;min-width:36px;text-align:center;line-height:36px;' href='{$url}'>{$v}</a>";
            }
        }
        if($this->config['prev']&&$this->nowPage>1){
            $query=$this->createUrl($this->nowPage-1);
            $url=$this->url.'?'.$query;
            $html="<a style='border:1px solid #e1e2e3;color:blue;text-decoration:none;margin:10px;display:inline-block;min-width:75px;text-align:center;line-height:36px;' href='{$url}'>上一页</a>".$html;
        }
        if($this->config['next']&&$this->nowPage<$this->totalPage){
            $query=$this->createUrl($this->nowPage+1);
            $url=$this->url.'?'.$query;
            $html.="<a style='border:1px solid #e1e2e3;color:blue;text-decoration:none;margin:10px;display:inline-block;min-width:75px;text-align:center;line-height:36px;' href='{$url}'>下一页</a>";
        }
        if($this->config['first']&&$this->nowPage>1){
            $query=$this->createUrl(1);
            $url=$this->url.'?'.$query;
            $html="<a style='border:1px solid #e1e2e3;color:blue;text-decoration:none;margin:10px;display:inline-block;min-width:75px;text-align:center;line-height:36px;' href='{$url}'>首页</a>".$html;
        }
        if($this->config['last']&&$this->nowPage<$this->totalPage){
            $query=$this->createUrl($this->totalPage);
            $url=$this->url.'?'.$query;
            $html.="<a style='border:1px solid #e1e2e3;color:blue;text-decoration:none;margin:10px;display:inline-block;min-width:75px;text-align:center;line-height:36px;' href='{$url}'>尾页</a>";
        }
        if($this->config['total']){
            $html.="<span style='margin:10px;display:inline-block;min-width:100px;text-align:center;line-height:36px;'>当前第{$this->nowPage}页,总{$this->totalPage}页</span>";
        }
        $html="<div style='text-align:right;padding:10px 5px;'>{$html}</div>";
        $this->html=$html;
    }
}
$page=new page(['prev'=>true,'next'=>true,'total'=>true,'first'=>true,'last'=>true]);
$nowPage=isset($_GET['page'])?$_GET['page']:1;
$html=$page->show($nowPage,2,1000);
echo $html;

新手写的一个分页思路,代码实现请忽略,仅用于做笔记,

这个思路关键在于

$half=intval($this->showPage/2);
$this->startPage=max(1,$this->nowPage-$half);
$this->endPage=min($this->startPage+$this->showPage-1,$this->totalPage);
$this->startPage=max(1,$this->endPage-$this->showPage+1);
$this->pageNum=range($this->startPage, $this->endPage);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:怎么去掉织梦网站首页带的index.html

下一篇:WordPress 4.9.1 添加备案号的正确姿势