php分页类及其实现原理
/**
*
* 实现思路:分页显示拆分 : 1...上页 12 13 14 15 [16] 17 18 19 20 ...100 下页
*
* function htmlPart1() : 上页
* function htmlPart2() : 1...
* function htmlPart3() : 12 13 14 15 [16] 17 18 19 20
* function htmlPart4() : ...100
* function htmlPart5() : 下页
*
* @param int $allCount 记录总数目
* @param int $eachPage 每页数目
* @param int $showCount 显示数目
* @param int $thenPage 当前页(页面传值)
* @param string $urlPrefix Url前缀
* @param string $urlSuffix Url后缀
*
* Author : intval@163.com
* Date : 2013.05.04
*
*/
class zPage { /** 只可内部调用 */
private $allCount; /** 总数(非总页数) */
private $eachPage; /** 每页数(分页数) */
private $showCount; /** 显示数(显示多少页数) */
private $urlPrefix; /** 页码前缀(例如:?page=) */
private $urlSuffix; /** 页码后缀缀(例如:&type=1) */
private $startHide; /** 计算前部需要出现符号(...)的最小值 */
private $endHide; /** 计算尾部需要出现符号(...)的最小值 */
private $arrTxt = array(' ', ' '); // array('上页', '上页') /** 可外部调用 */
public $allPage; /** 总页数 */
public $thenPage; /** 当前页 */ public function __construct($allCount, $eachPage, $showCount, $thenPage, $urlPrefix = '?page=', $urlSuffix = '') { $this->allCount = intval($allCount);
$this->eachPage = intval($eachPage);
$this->showCount = intval($showCount);
$this->urlPrefix = trim($urlPrefix);
$this->urlSuffix = trim($urlSuffix); /** 计算总页数 */
$this->allPage = ceil($this->allCount / $this->eachPage); /** 使当前页的数值合法化 */
$this->thenPage = max(intval($thenPage), 1);
$this->thenPage >= $this->allPage AND $this->thenPage = $this->allPage; /** 计算前部和尾部需要出现符号(...)的最小值 */
$this->startHide = ceil($this->showCount / 2);
$this->endHide = $this->allPage - $this->startHide;
} public function parseUrl($char = '') { $val = $char;
($char === 'prev') AND $val = $this->thenPage - 1;
($char === 'next') AND $val = $this->thenPage + 1;
($char === '') AND $val = $this->allPage;
return $this->urlPrefix . $val . $this->urlSuffix;
} public function htmlPart1() { $html = '';
$this->thenPage > $this->startHide AND $html = '<a class="prev" href="' . $this->parseUrl('prev') . '" title="上一页">' . $this->arrTxt[0] . '</a>' . PHP_EOL;
return $html;
} public function htmlPart2() { $dot = '';
$this->thenPage > $this->startHide AND $dot = ' ...'; $html = '<a href="' . $this->parseUrl(1) . '">1'. $dot .'</a>' . PHP_EOL;
$this->thenPage == 1 AND $html = '<span>1</span>' . PHP_EOL; return $html;
} public function htmlPart3() { $html = ''; if ($this->thenPage <= $this->startHide) { /**
* 第一种情况:[1] 2 3 4 5 6 7 8 9 10 ...100 下页
* 即:当前部在 不需要出现...的 范围之内
*/
for ($i = 2, $n = $this->showCount; $i < $n; $i++) {
$html .= (($i == $this->thenPage) ? '<span>' . $this->thenPage . '</span>' : '<a href="' . $this->parseUrl($i) . '">' . $i . '</a>') . PHP_EOL;
} } elseif ($this->thenPage >= $this->endHide) { /**
* 第二种情况:上页 1..92 93 94 95 96 97 98 [99] 100
* 即:当尾部在 不需要出现...的 范围之内
*/
$len = $this->showCount - 2;
$i = $this->allPage - $len;
$n = $this->allPage;
for ($i; $i < $n; $i++) {
$html .= (($i == $this->thenPage) ? '<span>' . $this->thenPage . '</span>' : '<a href="' . $this->parseUrl($i) . '">' . $i . '</a>') . PHP_EOL;
} } else { /**
* 第三种情况:上页 1..12 13 14 15 [16] 17 18 19 20 ...100 下页
* 即:当前后都在 需要出现...的 范围之内
*/
$len = $this->showCount - 2; // 此处减去2,是说明头尾各占去一个数字(1, x)
$offset = ceil($len / 2) - 1; // 对剩下的数目平分,得出平分数
$i = $this->thenPage - $offset; // 循环开始:当前页向前偏移平分数
$n = $this->thenPage + $offset; // 循环结束:当前页向后偏移平分数
for ($i; $i <= $n; $i++) {
$html .= (($i == $this->thenPage) ? '<span>' . $this->thenPage . '</span>' : '<a href="' . $this->parseUrl($i) . '">' . $i . '</a>') . PHP_EOL;
}
} return $html;
} public function htmlPart4() { $dot = '';
$this->thenPage < $this->endHide AND $dot = '... '; $html = '<a href="' . $this->parseUrl() . '">' . $dot . $this->allPage . '</a>' . PHP_EOL;
$this->thenPage == $this->allPage AND $html = '<span>' . $this->allPage . '</span>' . PHP_EOL; return $html;
} public function htmlPart5() { $html = '';
$this->thenPage < $this->endHide AND $html = '<a class="next" href="' . $this->parseUrl('next') . '" title="下一页">' . $this->arrTxt[1] . '</a>' . PHP_EOL;
return $html;
} public function html() { $pageHtml = ''; /** 总页数未达到显示页码数,则全部显示 */
if ($this->allPage <= $this->showCount) {
for ($i = 1; $i <= $this->allPage; $i++) {
$pageHtml .= ($i == $this->thenPage) ? '<span>' . $this->thenPage . '</span>' . PHP_EOL : '<a href="' . $this->parseUrl($i) . '">' . $i . '</a>' . PHP_EOL;
}
} else {
$pageHtml = $this->htmlPart2() . $this->htmlPart1() . $this->htmlPart3() . $this->htmlPart4() . $this->htmlPart5();
} return $pageHtml;
}
} // 调用例子
$getPage = isset($_GET['page']) ? intval($_GET['page']) : 0;
$getPage = max($getPage, 1);
$zPage = new zPage(1100, 10, 11, $getPage, '?page=', '&type=1');
echo $zPage->html();
php分页类及其实现原理的更多相关文章
- php分页类代码带分页样式效果(转)
php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...
- php+mysql分页类的入门实例
php+mysql分页类的简单代码 时间:2016-02-25 06:16:26来源:网络 导读:php+mysql分页类的简单代码,二个php分页类代码,对商品进行分页展示,当前页面数,每个页面展示 ...
- PHP面向对象(OOP)----分页类
> 同验证码类,分页也是在个人博客,论坛等网站中不可缺少的方式,通过分页可以在一个界面展示固定条数的数据,而不至于将所有数据全部罗列到一起,实现分页的原理其实就是对数据库查询输出加了一个limi ...
- php实现的分页类
php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...
- asp.net的快捷实用分页类
KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...
- php分页类
1.需求 学会php分页类的使用 2.参考例子 CI的分页类 3.代码部分 <?php class pagination{ public $pagesize=20; public $pagein ...
- PHPCMS V9 分页类的修改教程
首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...
- php 简单分页类
/** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- PHP简单漂亮的分页类
本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...
随机推荐
- Android 四大组件之Activity生命周期
写这篇博文之前,已经对android有一定的了解和认识.这篇博文主要讲述android的Activity的生命周期,这是android开发者必须掌握的知识.android的Activity组件拥有7个 ...
- Android博客
各版本SDK Tools及ADT下载技巧:http://www.cnblogs.com/zhjsll/p/5147553.html 深入浅出SlidingMenu:http://www.cnblogs ...
- iOS开发之网络请求(基于AFNetworking的再封装)
最近一直很忙也没有什么时间写博客了.放假了休息一下,就写一篇博客来总结一下最近做项目中出现过的问题吧!!! 首先,在项目中我的起到了什么作用,无非就是把美工(UI设计师)给我们的图显示出来,然后再和服 ...
- iOS开发那些事儿(六)Git分之策略
git 分支策略 将要介绍的这个模型不会比任何一套流程内容多,每个团队成员都必须遵守,这样便于管理软件开发过程. 既分散又集中 我们使用的,且与这个分支模型配合的非常好的库,他有一个“真正”的中央仓库 ...
- Android 分享微信好友 朋友圈
第三方应用,可以调用微信分享,把链接,文字,各种media,分享到微信好友或者微信朋友圈,步骤: package com.edaixi.utils; import android.content.Co ...
- C++进程间通信(常用理解例子)-买票
#include "stdafx.h" #include <iostream>using namespace std; #include "windows.h ...
- 初学swift笔记 函数(六)
import Foundation /* func 函数名 (参数名:参数类型) { } func 函数名 (参数名:参数类型) ->Void{ } func 函数名 (参数名:参数类型) -& ...
- 安装 Archlinux 小记
故事的背景 开始的时候装的 win8 + ubuntu 双系统,但是慢慢感觉只要有 windows 存在,在 Linux 上遇到问题了就想逃回去. 在一次 GDG 的演讲中听到的: 趁现在还年轻,还有 ...
- 用 Python脚本生成 Android SALT 扰码
发布Android 有偿应用时需要随机生成 SALT 扰码夹在文件中,以下是 Python脚本(当然你选择 C/Java/SHELL/Perl 或别的都行) #!/usr/bin/python # F ...
- C/C++源代码的Include依赖关系图
前一篇博文中我曾仔细介绍过如何查看C/C++代码的依赖项关系图,在这篇文章中我将会介绍如何使用Visualization and Modeling Feature Pack 工具包,查看C/C++源代 ...