php分页思路

<?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);

转载于:https://www.cnblogs.com/luwz/p/8435846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值