ThinkPHP 3.2.3自带的分页类位于:/ThinkPHP/Library/Think/Pages.class.php ,官方文档在这里:ThinkPHP3.2.3数据分页

Pages.class.php源码:

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. class Page{
  13. public $firstRow; // 起始行数
  14. public $listRows; // 列表每页显示行数
  15. public $parameter; // 分页跳转时要带的参数
  16. public $totalRows; // 总行数
  17. public $totalPages; // 分页总页面数
  18. public $rollPage = 11;// 分页栏每页显示的页数
  19. public $lastSuffix = true; // 最后一页是否显示总页数
  20. private $p = 'p'; //分页参数名
  21. private $url = ''; //当前链接URL
  22. private $nowPage = 1;
  23. // 分页显示定制
  24. private $config = array(
  25. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  26. 'prev' => '<<',
  27. 'next' => '>>',
  28. 'first' => '1...',
  29. 'last' => '...%TOTAL_PAGE%',
  30. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  31. );
  32. /**
  33. * 架构函数
  34. * @param array $totalRows 总的记录数
  35. * @param array $listRows 每页显示记录数
  36. * @param array $parameter 分页跳转的参数
  37. */
  38. public function __construct($totalRows, $listRows=20, $parameter = array()) {
  39. C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
  40. /* 基础设置 */
  41. $this->totalRows = $totalRows; //设置总记录数
  42. $this->listRows = $listRows; //设置每页显示行数
  43. $this->parameter = empty($parameter) ? $_GET : $parameter;
  44. $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  45. $this->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
  46. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  47. }
  48. /**
  49. * 定制分页链接设置
  50. * @param string $name 设置名称
  51. * @param string $value 设置值
  52. */
  53. public function setConfig($name,$value) {
  54. if(isset($this->config[$name])) {
  55. $this->config[$name] = $value;
  56. }
  57. }
  58. /**
  59. * 生成链接URL
  60. * @param integer $page 页码
  61. * @return string
  62. */
  63. private function url($page){
  64. return str_replace(urlencode('[PAGE]'), $page, $this->url);
  65. }
  66. /**
  67. * 组装分页链接
  68. * @return string
  69. */
  70. public function show() {
  71. if(0 == $this->totalRows) return '';
  72. /* 生成URL */
  73. $this->parameter[$this->p] = '[PAGE]';
  74. $this->url = U(ACTION_NAME, $this->parameter);
  75. /* 计算分页信息 */
  76. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  77. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  78. $this->nowPage = $this->totalPages;
  79. }
  80. /* 计算分页临时变量 */
  81. $now_cool_page = $this->rollPage/2;
  82. $now_cool_page_ceil = ceil($now_cool_page);
  83. $this->lastSuffix && $this->config['last'] = $this->totalPages;
  84. //上一页
  85. $up_row = $this->nowPage - 1;
  86. $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>' : '';
  87. //下一页
  88. $down_row = $this->nowPage + 1;
  89. $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>' : '';
  90. //第一页
  91. $the_first = '';
  92. if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
  93. $the_first = '<a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a>';
  94. }
  95. //最后一页
  96. $the_end = '';
  97. if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
  98. $the_end = '<a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>';
  99. }
  100. //数字连接
  101. $link_page = "";
  102. for($i = 1; $i <= $this->rollPage; $i++){
  103. if(($this->nowPage - $now_cool_page) <= 0 ){
  104. $page = $i;
  105. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  106. $page = $this->totalPages - $this->rollPage + $i;
  107. }else{
  108. $page = $this->nowPage - $now_cool_page_ceil + $i;
  109. }
  110. if($page > 0 && $page != $this->nowPage){
  111. if($page <= $this->totalPages){
  112. $link_page .= '<a class="num" href="' . $this->url($page) . '">' . $page . '</a>';
  113. }else{
  114. break;
  115. }
  116. }else{
  117. if($page > 0 && $this->totalPages != 1){
  118. $link_page .= '<span class="current">' . $page . '</span>';
  119. }
  120. }
  121. }
  122. //替换分页内容
  123. $page_str = str_replace(
  124. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  125. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  126. $this->config['theme']);
  127. return "<div>{$page_str}</div>";
  128. }
  129. }

调用Page类的show()方法后,返回的是拼接好的分页HTML信息。注意到这里提供了theme可供配置,利用theme可以改变返回的HTML信息样式。

以theme为"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%"为例,返回的是(格式化显示):

  1. <div>
  2. <a class="first" href="xxx">xxx</a>
  3. <a class="prev" href="xxx">xxx</a>
  4. <a class="num" href="xxx">xxx</a>
  5. <span class="current" href="xxx">xxx</span>
  6. <a class="next" href="xxx">xxx</a>
  7. <a class="end" href="xxx">xxx</a>
  8. </div>

在看Bootstrap的分页组件:

  1. <ul class="pagination">
  2. <li>
  3. <a href="#" aria-label="Previous">
  4. <span aria-hidden="true">&laquo;</span>
  5. </a>
  6. </li>
  7. <li><a href="#">1</a></li>
  8. <li><a href="#">2</a></li>
  9. <li><a href="#">3</a></li>
  10. <li><a href="#">4</a></li>
  11. <li><a href="#">5</a></li>
  12. <li>
  13. <a href="#" aria-label="Next">
  14. <span aria-hidden="true">&raquo;</span>
  15. </a>
  16. </li>
  17. </ul>

可以看出,对于默认分页组件,只要在外层套上ul.pagination,并将每一项套上li就行。

那么设置theme为:

  1. "<ul class="pagination"><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li></ul>"

就可以得到返回值(格式化显示):

  1. <div>
  2. <ul class="pagination">
  3. <li><a class="first" href="xxx">xxx</a></li>
  4. <li><a class="prev" href="xxx">xxx</a></li>
  5. <li>
  6. <a class="num" href="xxx">xxx</a>
  7. <span class="current" href="xxx">xxx</span>
  8. </li>
  9. <li><a class="next" href="xxx">xxx</a></li>
  10. <li><a class="end" href="xxx">xxx</a></li>
  11. </ul>
  12. </div>

最终效果如图:

采用配置theme可以不改变源码的情况下做到bootstrap默认分页风格。其他风格的需要对源码进行修改或自定义css风格。

使ThinkPHP(3.2.3)的分页类支持Bootstrap风格的更多相关文章

  1. PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...

  2. PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...

  3. PHP分页类,支持自定义样式,中间5页

    <?php //namespace Component; /** * 2016-3-27 * @author ankang */ class Page { private $ShowPage; ...

  4. thinkphp简洁、美观、靠谱的分页类

    我们要实现如下图分页效果 这个分页类是在thinkphp框架内置的分页类的基础上修改而来:原分页类的一些设计,在实际运用中感觉不是很方便: 1.只有一页内容时不显示分页: 2.原分页类在当前页是第一页 ...

  5. ThinkPHP 分页类的使用及退出功能的实现

    /* ThinkPHP设置编码统一: 一.数据库设置为utf8_bin 二.HTML页面设置charset=utf-8,而且检查文档编码格式是否是utf-8.phpDesigner8设置方式为“文件- ...

  6. (转)ThinkPHP使用心得分享-分页类Page的用法

    转之--http://www.jb51.net/article/50138.htm ThinkPHP中的Page类在ThinkPHP/Extend/Library/ORG/Util/Page.clas ...

  7. thinkphp 原生sql使用分页类

    public function index(){ import("@.ORG.Page"); //导入分页类 $Dao = M(); $count = count($Dao-> ...

  8. thinkphp 分页类 url 编码处理

    在做thinkphp分页的时候  thinkphp 中的分页 有一个小问题 就是 在有form 表单 搜索中文的时候,点击下一页的话 中文会被转换成编码. 如图: 最直接的方法就是 直接修改 thin ...

  9. ThinkPHP讲解(十)——第三方类的引入:以分页为主

    第三方类的引入,以分页类为例: 1.在控制器里新建一个分页的操作方法FenYe() 注意:第三方类Page.class.php放在Think或Home文件夹下,并新近一个文件夹,放在里面,并在其类里加 ...

随机推荐

  1. Ext sqlserver C# 数据库备份还原代码,给大家参考下

      <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %& ...

  2. 状压DP问题

    状态压缩·一 题目传送:#1044 : 状态压缩·一 AC代码: #include <map> #include <set> #include <list> #in ...

  3. zabbix如何添加主机监控

    1,首先,监控的主机安装zabbix客户端.zabbix提供多种监控方式,我们这里监控的主机上边安装agentd守护端进行数据收集并监测. 其中客户端安装我们这里就不介绍了,请参考之前教程里边的客户端 ...

  4. 训练深度学习网络时候,出现Nan是什么原因,怎么才能避免?——我自己是因为data有nan的坏数据,clear下解决

    from:https://www.zhihu.com/question/49346370   Harick     梯度爆炸了吧. 我的解决办法一般以下几条:1.数据归一化(减均值,除方差,或者加入n ...

  5. mybatis 注解写法 多层嵌套foreach,调用存储过程,批量插入数据

    @Select("<script>" + "DECLARE @edi_Invoice_Details edi_Invoice_Details;" + ...

  6. PCB SI9000阻抗计算引擎Web方式实现方法

    在笔者从业这么多年,PCB行业阻抗计算工具都是用Polar公司的阻抗计算工具SI9000,或早期上个版 本SI8000 Prolar是老牌公司,但也不断在推出新的产品,可以进去去了解一下   http ...

  7. bzoj 1618: [Usaco2008 Nov]Buying Hay 购买干草【背包】

    好像是完全背包吧分不清了-- 好像是把数组二维压一维的时候,01背包倒序,完全背包正序 ```cpp include include using namespace std; const int N= ...

  8. Wannafly挑战赛29A御坂美琴

    传送门 这套题很有意思2333 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些数,用map记录一下,判断是否所有数都能被分出来就是了 //m ...

  9. CentOS 6.5克隆后eth1与eth0的问题

    CentOS 6.5克隆后eth1与eth0的问题   按照安装文档执行以下步骤时:   从克隆出来的虚拟机网卡都会被命名为eth1,而有些程序或者脚本,涉及到网卡的,默认写的是eth0,这时就存在要 ...

  10. 测试神器Swagger的相关使用

    1.Swagger简介 swagger官网地址: https://swagger.io/ swagger官网文档介绍地址: https://swagger.io/about/ ​ swagge是一个易 ...