PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Library/Org/Page.class.php

https://gitlab.com/xuebutayan/yshop
PHP | 154 lines | 110 code | 8 blank | 36 comment | 22 complexity | 8d9280438b4718c5d1b395ed5bac858a 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. // | lanfengye <zibin_5257@163.com>
  11. // +----------------------------------------------------------------------
  12. class Page {
  13. // 分页栏每页显示的页数
  14. public $rollPage = 5;
  15. // 页数跳转时要带的参数
  16. public $parameter ;
  17. // 分页URL地址
  18. public $url = '';
  19. // 默认列表每页显示行数
  20. public $listRows = 20;
  21. // 起始行数
  22. public $firstRow ;
  23. // 分页总页面数
  24. protected $totalPages ;
  25. // 总行数
  26. protected $totalRows ;
  27. // 当前页数
  28. protected $nowPage ;
  29. // 分页的栏的总页数
  30. protected $coolPages ;
  31. // 分页显示定制
  32. protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');
  33. // 默认分页变量名
  34. protected $varPage;
  35. /**
  36. * 架构函数
  37. * @access public
  38. * @param array $totalRows 总的记录数
  39. * @param array $listRows 每页显示记录数
  40. * @param array $parameter 分页跳转的参数
  41. */
  42. public function __construct($totalRows,$listRows='',$parameter='',$url='') {
  43. $this->totalRows = $totalRows;
  44. $this->parameter = $parameter;
  45. $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
  46. if(!empty($listRows)) {
  47. $this->listRows = intval($listRows);
  48. }
  49. $this->totalPages = ceil($this->totalRows/$this->listRows); //总页数
  50. $this->coolPages = ceil($this->totalPages/$this->rollPage);
  51. $this->nowPage = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
  52. if($this->nowPage<1){
  53. $this->nowPage = 1;
  54. }elseif(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  55. $this->nowPage = $this->totalPages;
  56. }
  57. $this->firstRow = $this->listRows*($this->nowPage-1);
  58. if(!empty($url)) $this->url = $url;
  59. }
  60. public function setConfig($name,$value) {
  61. if(isset($this->config[$name])) {
  62. $this->config[$name] = $value;
  63. }
  64. }
  65. /**
  66. * 分页显示输出
  67. * @access public
  68. */
  69. public function show() {
  70. if(0 == $this->totalRows) return '';
  71. $p = $this->varPage;
  72. $nowCoolPage = ceil($this->nowPage/$this->rollPage);
  73. // 分析分页参数
  74. if($this->url){
  75. $depr = C('URL_PATHINFO_DEPR');
  76. $url = rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__';
  77. }else{
  78. if($this->parameter && is_string($this->parameter)) {
  79. parse_str($this->parameter,$parameter);
  80. }elseif(is_array($this->parameter)){
  81. $parameter = $this->parameter;
  82. }elseif(empty($this->parameter)){
  83. unset($_GET[C('VAR_URL_PARAMS')]);
  84. $var = !empty($_POST)?$_POST:$_GET;
  85. if(empty($var)) {
  86. $parameter = array();
  87. }else{
  88. $parameter = $var;
  89. }
  90. }
  91. $parameter[$p] = '__PAGE__';
  92. $url = U('',$parameter);
  93. }
  94. //上下翻页字符串
  95. $upRow = $this->nowPage-1;
  96. $downRow = $this->nowPage+1;
  97. if ($upRow>0){
  98. $upPage = "<a href='".str_replace('__PAGE__',$upRow,$url)."'>".$this->config['prev']."</a>";
  99. }else{
  100. $upPage = '';
  101. }
  102. if ($downRow <= $this->totalPages){
  103. $downPage = "<a href='".str_replace('__PAGE__',$downRow,$url)."'>".$this->config['next']."</a>";
  104. }else{
  105. $downPage = '';
  106. }
  107. // << < > >>
  108. if($nowCoolPage == 1){
  109. $theFirst = '';
  110. $prePage = '';
  111. }else{
  112. $preRow = $this->nowPage-$this->rollPage;
  113. $prePage = "<a href='".str_replace('__PAGE__',$preRow,$url)."' >上".$this->rollPage."页</a>";
  114. $theFirst = "<a href='".str_replace('__PAGE__',1,$url)."' >".$this->config['first']."</a>";
  115. }
  116. if($nowCoolPage == $this->coolPages){
  117. $nextPage = '';
  118. $theEnd = '';
  119. }else{
  120. $nextRow = $this->nowPage+$this->rollPage;
  121. $theEndRow = $this->totalPages;
  122. $nextPage = "<a href='".str_replace('__PAGE__',$nextRow,$url)."' >下".$this->rollPage."页</a>";
  123. $theEnd = "<a href='".str_replace('__PAGE__',$theEndRow,$url)."' >".$this->config['last']."</a>";
  124. }
  125. // 1 2 3 4 5
  126. $linkPage = "";
  127. for($i=1;$i<=$this->rollPage;$i++){
  128. $page = ($nowCoolPage-1)*$this->rollPage+$i;
  129. if($page!=$this->nowPage){
  130. if($page<=$this->totalPages){
  131. $linkPage .= "<a href='".str_replace('__PAGE__',$page,$url)."'>".$page."</a>";
  132. }else{
  133. break;
  134. }
  135. }else{
  136. if($this->totalPages != 1){
  137. $linkPage .= "<span class='current'>".$page."</span>";
  138. }
  139. }
  140. }
  141. $pageStr = str_replace(
  142. array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
  143. array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
  144. return $pageStr;
  145. }
  146. }