Page.class.php 5.8 KB

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