Page.class.php 5.8 KB

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