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后端给的接口,以实现分页的功能.由于我是没造轮子的能力,所以翻了不少技术博客,经过整合才算完成整个分页功能.从一番查阅中,不难看出大概分为两种不同的分页: 一种是纯前端的,就是在一次 ...
随机推荐
- 爬虫之解析库BeautifulSoup
介绍 Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等 ...
- CF1214B
CF1214B 解法: 暴力枚举,时间复杂度 $ O(n) $ CODE: #include<iostream> #include<cstdio> #include<cs ...
- elasticsearch shield(5.0以下版本 权限认证)
elasticsearch 5.0以下的版本要用到权限控制的话需要使用shield.下载地址: https://www.elastic.co/downloads/shield5.0以上的版本则可以使用 ...
- Qt for Android (二) Qt打开android的相册
以下有一个可以用的Demo https://files.cnblogs.com/files/wzxNote/Qt-Android-Gallery-master.zip 网盘地址: https://pa ...
- Python 调用系统命令的模块 Subprocess
Python 调用系统命令的模块 Subprocess 有些时候需要调用系统内部的一些命令,或者给某个应用命令传不定参数时可以使用该模块. 初识 Subprocess 模块 Subprocess 模块 ...
- servlet多线程安全问题
Servelet多线程安全问题 原因 一个servlet被实例化一次,当有多个用户访问时,多个线程会访问同一个实例,实例变量就会被不同的用户修改. 简单的案例 新建servlet,访问http://l ...
- WEB-INF目录下登录表单提交的重定向
问题描述 登陆表单提交跳转后刷新会重新提交表单,但是使用重定向时不走视图解析器,不能访问WEB-INF下的资源 解决方法 原方法 @RequestMapping(value = "/logi ...
- JS模拟Touch事件
var ele = document.getElementsByClassName('target_node_class')[0] //may have x and y properties in s ...
- win10windows无法创建快捷方式 请检查磁盘
这个是因为文件没有权限造成的 打开"我的电脑",菜单栏里选择"工具"-"文件夹选项"-"查看",把里面" ...
- UML期末复习题——2.9:UML Deployment Diagram
附加题:部署图 重要概念: 1. 部署图 部署图表示的是,如何将具体的软件制品(例如可执行文件)分配到计算节点(具有处理服务的某种事物)上.部署图表示了软件元素在物理架构上的部署,以及物理元素之间的通 ...