1. 程式人生 > >php分頁思路

php分頁思路

total center idt unset return rtp isset fig 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);

php分頁思路