<?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分页思路的更多相关文章

  1. hbase分页应用场景及分页思路与代码实现

    转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=7030&extra=page=1 可以带着下面问题来阅读1.hbase ...

  2. 记录--java 分页 思路 (hibernate关键代码)

    有时会脑袋蒙圈,记录下分页的思路 下面代码是hibernate的分页,其分页就是从第几条数据为起点,取几条数据.比如在mysql中的limit(5,10)取的就是第6条到第10条 在下面代码中的pag ...

  3. redis缓存分页思路

    传统分页一般分页做缓存都是直接查找出来,按页放到缓存里,但是这种缓存方式有很多缺点.如缓存不能及时更新,一旦数据有变化,所有的之前的分页缓存都失效了.比如像微博这样的场景,微博下面现在有一个顶次数的排 ...

  4. 黑马day13 分页思路&amp;实现

    分页的总体思想: 分页包含什么: 1.当前页,每页显示的记录数,总的记录数,总的页码,集合List存放的是JavaBean,首页, 尾页,上一页,下一页 传递的參数:当前页,每页显示的记录数.这两个本 ...

  5. Python 简单分页思路

    一: li = [] for i in range(1000): li.append(i) while True: p = input('input page: ') p = int(p) start ...

  6. [MySQL] 分页优化

    在传统的分页思路影响下,很多人都形成了对于分页的固定理解,也就是给出select语句,先用count()函数计算出总的条目,除与每个页面大小pagesize,然后用ceil取整,得出总的页数,用lim ...

  7. SQL Server中的分页

    sqlserver2000时的分页思路 .分页查询时,首先将数据排序 select * from MyStudent order by fid desc .取第一页数据 * from MyStuden ...

  8. SQLServer分页

    1.为什么要分页? 当显示数据的时候,我们不会一下子把所有的数据都显示出来,比如说表中有一万条数据,难道我们要把一万条数据都一次性的显示出来吗?!即便显示给用户了,用户也看不过来.因此,不论是从效率的 ...

  9. RE: Javascript分页处理

    背景: 调用PHP后端给的接口,以实现分页的功能.由于我是没造轮子的能力,所以翻了不少技术博客,经过整合才算完成整个分页功能.从一番查阅中,不难看出大概分为两种不同的分页: 一种是纯前端的,就是在一次 ...

随机推荐

  1. hive的两种使用方式

    hive的两种使用方式 1,hive shell的方式 启动命令: bin/hive 2.beeline客户端方式 首先在一个机器上启动hive thrift服务 bin/hiveserver2 在其 ...

  2. RuntimeException和Exception的区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  3. idea 2018注册码(激活码)

    最近做一个项目,用idea 社区版的   但是缺少了好多功能 无奈只能用专业版的,但是需要注册激活  下面是我的注册方法 1.打开了idea  会提示让激活  选择Licensse server 2. ...

  4. Java排序之计数排序

    Java排序之计数排序 计数排序思路 计数排序适用于有明确范围的数组,比如给定一个数组,且知道所有值得范围是[m,n].这个时候可以使用一个n-m+1长度的数组,待排序的数组就可以散在这个数组上,数组 ...

  5. Shiro RememberMe 1.2.4远程代码执行漏洞-详细分析

    本文首发于先知: https://xz.aliyun.com/t/6493 0x01.漏洞复现 环境配置 https://github.com/Medicean/VulApps/tree/master ...

  6. SQLyog Enterprise Trial 试用期问题

    SQLyog Enterprise Trial 是 SQLyog的试用版,有效期30天:试用期过后提示购买之后才能使用:解决办法:修改注册表(过期就得改比较麻烦,但暂时可以用,等有时间了再找其他办法) ...

  7. LightGBM两种使用方式

    原生形式使用lightgbm(import lightgbm as lgb) import lightgbm as lgb from sklearn.metrics import mean_squar ...

  8. UML期末复习题——2.5:System Sequence Diagram & Post-condition

    第五题:系统顺序图 重要概念: 1.对象: 对象是特定行为与属性的集合. 对象的表示方式有三种: a.包括对象名和类名 b.只有类名. c.只有对象名 2.消息表示形式: 消息用于描述对象间交互的方式 ...

  9. Vue中可用的判断对象是否为空的方法

    vue有两个方法可用 1. JSON.stringify(evtValue)=='{}' 2. Object.keys(xxx).length==0 js判断对象是否为空对象的几种方法 1.将json ...

  10. SQL-W3School-高级:SQL VIEW(视图)

    ylbtech-SQL-W3School-高级:SQL VIEW(视图) 1.返回顶部 1. 视图是可视化的表. 本章讲解如何创建.更新和删除视图. SQL CREATE VIEW 语句 什么是视图? ...