php分页思路
2018-06-22 05:35:47来源:未知 阅读 ()
<?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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash