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分页思路的更多相关文章
- hbase分页应用场景及分页思路与代码实现
转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=7030&extra=page=1 可以带着下面问题来阅读1.hbase ...
- 记录--java 分页 思路 (hibernate关键代码)
有时会脑袋蒙圈,记录下分页的思路 下面代码是hibernate的分页,其分页就是从第几条数据为起点,取几条数据.比如在mysql中的limit(5,10)取的就是第6条到第10条 在下面代码中的pag ...
- redis缓存分页思路
传统分页一般分页做缓存都是直接查找出来,按页放到缓存里,但是这种缓存方式有很多缺点.如缓存不能及时更新,一旦数据有变化,所有的之前的分页缓存都失效了.比如像微博这样的场景,微博下面现在有一个顶次数的排 ...
- 黑马day13 分页思路&实现
分页的总体思想: 分页包含什么: 1.当前页,每页显示的记录数,总的记录数,总的页码,集合List存放的是JavaBean,首页, 尾页,上一页,下一页 传递的參数:当前页,每页显示的记录数.这两个本 ...
- Python 简单分页思路
一: li = [] for i in range(1000): li.append(i) while True: p = input('input page: ') p = int(p) start ...
- [MySQL] 分页优化
在传统的分页思路影响下,很多人都形成了对于分页的固定理解,也就是给出select语句,先用count()函数计算出总的条目,除与每个页面大小pagesize,然后用ceil取整,得出总的页数,用lim ...
- SQL Server中的分页
sqlserver2000时的分页思路 .分页查询时,首先将数据排序 select * from MyStudent order by fid desc .取第一页数据 * from MyStuden ...
- SQLServer分页
1.为什么要分页? 当显示数据的时候,我们不会一下子把所有的数据都显示出来,比如说表中有一万条数据,难道我们要把一万条数据都一次性的显示出来吗?!即便显示给用户了,用户也看不过来.因此,不论是从效率的 ...
- RE: Javascript分页处理
背景: 调用PHP后端给的接口,以实现分页的功能.由于我是没造轮子的能力,所以翻了不少技术博客,经过整合才算完成整个分页功能.从一番查阅中,不难看出大概分为两种不同的分页: 一种是纯前端的,就是在一次 ...
随机推荐
- http接口测试工具 REST Client
以下几个工具为常用rest测试工具 1.Postman, 可以下载客户端或者安装浏览器插件 2.Insomnia,window客户端 3.Apizza,在线极客专属的api协作管理工具 ...
- Atcoder ABC 139E
Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...
- 对@repository,@Service, @Compent,@Controller注解的理解
注解是没什么本质区别,都是声明作用,取不同的名字只是为了更好区分各自的功能. @Repository 用于标注数据访问组件,即DAO组件 @Service 用于标注业务层组件 @Controller ...
- iTerm2 + oh my zsh +agnoster
安装iTerm2 iTerm2官方下载地址 http://www.iterm2.com/downloads.html 安装Oh My Bash 1.通过cat /etc/shells命令可以查看当前系 ...
- spark-submit 参数总结
spark-submit 可以提交任务到 spark 集群执行,也可以提交到 hadoop 的 yarn 集群执行. 1)./spark-shell --help :不知道如何使用,可通过它查看命 ...
- kotlin嵌套类
就是类中定义类 package loaderman.demo class Outer { var name: String = "name" inner class inner { ...
- [Scikit-learn] 1.1 Generalized Linear Models - Comparing various online solvers
数据集分割 一.Online learning for 手写识别 From: Comparing various online solvers An example showing how diffe ...
- Python实现按照指定要求逆序输出一个数字的方法
Python实现按照指定要求逆序输出一个数字的方法 这篇文章主要介绍了Python实现按照指定要求逆序输出一个数字的方法,涉及Python针对字符串的遍历.判断.输出等相关操作技巧,需要的朋友可以参考 ...
- Linux下如何启用MySQL数据库远程访问
远程连接MySQL出于安全考虑,一般都关闭了远程访问,但有时候需要提供远程访问数据库的服务,下面我们快速学习下: 第一步:修改my.cnf文件使用文本编辑器去编辑MySQL服务器的配置文件my.cnf ...
- 使用 bash 脚本把 GCE 的数据备份到 GCS
目录 一.Google Cloud Storge 介绍 1.1.四种存储类别的比较 1.2.需求 1.3.给虚拟机添加授权认证 二.备份操作 2.1 创建存储分区 2.2 上传对象到存储分区 2.3 ...