/frogphp/Lib/Extend/Page.class.php

https://bitbucket.org/silenceper/frogphp · PHP · 130 lines · 93 code · 6 blank · 31 comment · 18 complexity · 5b41db49cd95493a40f7dfbcf9349ff0 MD5 · raw file

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