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后端给的接口,以实现分页的功能.由于我是没造轮子的能力,所以翻了不少技术博客,经过整合才算完成整个分页功能.从一番查阅中,不难看出大概分为两种不同的分页: 一种是纯前端的,就是在一次 ...
随机推荐
- 7.27T2
不可做题 sol:首先有个很显然的性质就是答案一定是在叶子上最优,然后画画图发现就是从最底层看,如果一条链就看做一个点,向上的第一颗非链的节点,它的儿子数-1就会对答案贡献,所有这样的累加起来就是答案 ...
- c 判断是否为 字母或数字(iswalnum example)
#include <stdio.h> #include <wctype.h> int main () { int i; wchar_t str[] = L"c3p.o ...
- MySQL中使用LIMIT分页
需求:客户端通过传递pageNum(页码)和pageSize(每页显示的条数)两个参数去分页查询数据库表中的数据. 我们知道MySQL提供了分页函数limit m,n,但是该函数的用法和需求不一样,所 ...
- LDA线性分析推广到多分类
感谢皮果提的文章: http://blog.csdn.net/itplus/article/details/12038441 http://blog.csdn.net/itplus/article 皮 ...
- 第11组 Alpha冲刺(4/6)
第11组 Alpha冲刺(4/6) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11884529.html 作业博客 https://edu ...
- 通过sqlserver用户操作远程服务器
USE masterGORECONFIGURE --先执行一次刷新,处理上次的配置GOEXEC sp_configure 'show advanced options',1 --启用xp_cmdshe ...
- 【java】[sql]使用Java程序向MySql数据库插入一千万条记录,各种方式的比较,最后发现insert批量插入方式对效率提升最明显
我的数据库环境是mysql Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper 这个数据库是安装在T440p的虚拟机 ...
- kotlin中集合
fun main(arg: Array<String>) { //可读写的集合创建 val mutableListOf1 = mutableListOf<Int>(1, 2, ...
- Mysql5.6.45配置安装
##1.1简介 因为之前电脑比较卡,自己重装了个系统,麻烦的就是装一些编程软件,但是自己吃饭的家伙也要会装啊,为了以后更方便的安装,自己总结一下步骤,进入正题 ##1.2资源下载 官方网站链接: ...
- 广告行业——数字广告营销中的DSP、SSP、RTB是个什么概念
原文链接1 原文链接2 如我是一个创业公司,没啥钱,老板给了1000块钱预算,让我去投互联网广告,对我说: “小卡啊!给你1000块钱,我知道你没见过那么多的巨款吧!不要被吓着,尽情去挥霍吧!哦对了, ...