/Kernel/ThinkPHP/Lib/ORG/Util/Page.class.php

https://github.com/liujinsong668/epptime · PHP · 137 lines · 91 code · 6 blank · 40 comment · 17 complexity · 8f3f6d59f27f437d94570ab971552e5d MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. class Page extends Think {
  13. // 起始行数
  14. public $firstRow ;
  15. // 列表每页显示行数
  16. public $listRows ;
  17. // 页数跳转时要带的参数
  18. public $parameter ;
  19. // 分页总页面数
  20. protected $totalPages ;
  21. // 总行数
  22. protected $totalRows ;
  23. // 当前页数
  24. protected $nowPage ;
  25. // 分页的栏的总页数
  26. protected $coolPages ;
  27. // 分页栏每页显示的页数
  28. protected $rollPage ;
  29. // 分页显示定制
  30. protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');
  31. /**
  32. +----------------------------------------------------------
  33. * 架构函数
  34. +----------------------------------------------------------
  35. * @access public
  36. +----------------------------------------------------------
  37. * @param array $totalRows 总的记录数
  38. * @param array $listRows 每页显示记录数
  39. * @param array $parameter 分页跳转的参数
  40. +----------------------------------------------------------
  41. */
  42. public function __construct($totalRows,$listRows,$parameter='') {
  43. $this->totalRows = $totalRows;
  44. $this->parameter = $parameter;
  45. $this->rollPage = C('PAGE_ROLLPAGE');
  46. $this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
  47. $this->totalPages = ceil($this->totalRows/$this->listRows); //总页数
  48. $this->coolPages = ceil($this->totalPages/$this->rollPage);
  49. $this->nowPage = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1;
  50. if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  51. $this->nowPage = $this->totalPages;
  52. }
  53. $this->firstRow = $this->listRows*($this->nowPage-1);
  54. }
  55. public function setConfig($name,$value) {
  56. if(isset($this->config[$name])) {
  57. $this->config[$name] = $value;
  58. }
  59. }
  60. /**
  61. +----------------------------------------------------------
  62. * 分页显示输出
  63. +----------------------------------------------------------
  64. * @access public
  65. +----------------------------------------------------------
  66. */
  67. public function show() {
  68. if(0 == $this->totalRows) return '';
  69. $p = C('VAR_PAGE');
  70. $nowCoolPage = ceil($this->nowPage/$this->rollPage);
  71. $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  72. $parse = parse_url($url);
  73. if(isset($parse['query'])) {
  74. parse_str($parse['query'],$params);
  75. unset($params[$p]);
  76. $url = $parse['path'].'?'.http_build_query($params);
  77. }
  78. //上下翻页字符串
  79. $upRow = $this->nowPage-1;
  80. $downRow = $this->nowPage+1;
  81. if ($upRow>0){
  82. $upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
  83. }else{
  84. $upPage="";
  85. }
  86. if ($downRow <= $this->totalPages){
  87. $downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
  88. }else{
  89. $downPage="";
  90. }
  91. // << < > >>
  92. if($nowCoolPage == 1){
  93. $theFirst = "";
  94. $prePage = "";
  95. }else{
  96. $preRow = $this->nowPage-$this->rollPage;
  97. $prePage = "<a href='".$url."&".$p."=$preRow' >上".$this->rollPage."页</a>";
  98. $theFirst = "<a href='".$url."&".$p."=1' >".$this->config['first']."</a>";
  99. }
  100. if($nowCoolPage == $this->coolPages){
  101. $nextPage = "";
  102. $theEnd="";
  103. }else{
  104. $nextRow = $this->nowPage+$this->rollPage;
  105. $theEndRow = $this->totalPages;
  106. $nextPage = "<a href='".$url."&".$p."=$nextRow' >下".$this->rollPage."页</a>";
  107. $theEnd = "<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>";
  108. }
  109. // 1 2 3 4 5
  110. $linkPage = "";
  111. for($i=1;$i<=$this->rollPage;$i++){
  112. $page=($nowCoolPage-1)*$this->rollPage+$i;
  113. if($page!=$this->nowPage){
  114. if($page<=$this->totalPages){
  115. $linkPage .= "&nbsp;<a href='".$url."&".$p."=$page'>&nbsp;".$page."&nbsp;</a>";
  116. }else{
  117. break;
  118. }
  119. }else{
  120. if($this->totalPages != 1){
  121. $linkPage .= "&nbsp;<span class='current'>".$page."</span>";
  122. }
  123. }
  124. }
  125. $pageStr = str_replace(
  126. array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
  127. array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
  128. return $pageStr;
  129. }
  130. }
  131. ?>