php分页思路

2018-02-09 19:23:06来源:cnblogs.com作者:柠檬v不萌人点击

分享
<?phpclass 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);

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台