php分頁類代碼,支援多樣式可設定分頁方式

來源:互聯網
上載者:User
  1. class Page {

  2. private $total;//總數量
  3. private $limit;//返回mysql的limit語句
  4. private $pageStart;//開始的數值
  5. private $pageStop;//結束的數值
  6. private $pageNumber;//顯示分頁數位數量
  7. private $page;//當前頁
  8. private $pageSize;//每頁顯示的數量
  9. private $pageToatl;//分頁的總數量
  10. private $pageParam;//分頁變數
  11. private $uri;//URL參數
  12. /**
  13. * 分頁設定樣式 不區分大小寫
  14. * %pageToatl% //總頁數
  15. * %page%//當前頁
  16. * %pageSize% //當前頁顯示資料條數
  17. * %pageStart%//本頁起始條數
  18. * %pageStop%//本頁結束條數
  19. * %total%//總資料條數
  20. * %first%//首頁
  21. * %ending%//尾頁
  22. * %up%//上一頁
  23. * %down%//下一頁
  24. * %F%//起始頁
  25. * %E%//結束頁
  26. * %omitFA%//前省略加跳轉
  27. * %omitEA%//後省略加跳轉
  28. * %omitF%//前省略
  29. * %omitE%//後省略
  30. * %numberF%//固定數量的數字分頁
  31. * %numberD%//左右對等的數字分頁
  32. * %input%//跳轉輸入框
  33. * %GoTo%//跳轉按鈕
  34. */ bbs.it-home.org
  35. private $pageType = '第%page%頁/共%pageToatl%頁%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
  36. //顯示值設定
  37. private $pageShow = array('first'=>'首頁','ending'=>'尾頁','up'=>'上一頁','down'=>'下一頁','GoTo'=>'GO');

  38. /**

  39. * 初始化資料,構造方法
  40. * @access public
  41. * @param int $total 資料總數量
  42. * @param int $pageSize 每頁顯示條數
  43. * @param int $pageNumber 分頁數字顯示數量(使用%numberF%和使用%numberD%會有不同的效果)
  44. * @param string $pageParam分頁變數
  45. * @return void
  46. */
  47. public function __construct($total,$pageSize=10,$pageNumber=5,$pageParam='p'){
  48. $this->total = $total<0?0:$total;
  49. $this->pageSize = $pageSize<0?0:$pageSize;
  50. $this->pageNumber = $pageNumber<0?0:$pageNumber;
  51. $this->pageParam = $pageParam;
  52. $this->calculate();
  53. }

  54. /**

  55. * 顯示分頁
  56. * @access public
  57. * @return string HTML分頁字串
  58. */
  59. public function pageShow(){
  60. $this->uri();
  61. if($this->pageToatl>1){
  62. if($this->page == 1){
  63. $first = ''.$this->pageShow['first'].'';
  64. $up = ''.$this->pageShow['up'].'';
  65. }else{
  66. $first = 'uri.'&'.$this->pageParam.'=1">'.$this->pageShow['first'].'';
  67. $up = 'uri.'&'.$this->pageParam.'='.($this->page-1).'">'.$this->pageShow['up'].'';
  68. }
  69. if($this->page >= $this->pageToatl){
  70. $ending = ''.$this->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'';
  72. }else{
  73. $ending = 'uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageShow['ending'].'';
  74. $down = 'uri.'&'.$this->pageParam.'='.($this->page+1).'">'.$this->pageShow['down'].'';
  75. }
  76. $input = '';
  77. $GoTo = ''.$this->pageShow['GoTo'].'';
  78. }else{
  79. $first = '';$up ='';$ending = '';$down = '';$input = '';$GoTo = '';
  80. }
  81. $this->numberF();
  82. return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
  83. }

  84. /**

  85. *數字分頁
  86. */
  87. private function numberF(){
  88. $pageF = stripos($this->pageType,'%numberF%');
  89. $pageD = stripos($this->pageType,'%numberD%');
  90. $numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
  91. if($pageF!==false || $pageD!==false){
  92. if($pageF!==false){
  93. $number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
  94. $DStart = $this->page - $number<0?$this->page - $number-1:0;
  95. if($this->page+$number-$DStart > $this->pageToatl){
  96. $DStop = ($this->page+$number-$DStart) - $this->pageToatl;
  97. $forStop = $this->pageToatl+1;
  98. }else{
  99. $DStop = 0;
  100. $forStop = $this->page+$number-$DStart+1;
  101. }
  102. $forStart = $this->page-$number-$DStop<1?1:$this->page-$number-$DStop;
  103. for($i=$forStart;$i<$forStop;++$i){
  104. if($i==$this->page){
  105. $numberF .= ''.$i.'';
  106. }else{
  107. $numberF .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  108. }
  109. }
  110. }
  111. if($pageD!==false){
  112. $number = $this->pageNumber;
  113. $forStart = $this->page-$number>0?$this->page-$number:1;
  114. $forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
  115. for($i=$forStart;$i<$this->page;++$i){
  116. $numberD .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  117. }
  118. $numberD .= ''.$this->page.'';
  119. $start = $this->page+1;
  120. for($i=$start;$i<$forStop;++$i){
  121. $numberD .= 'uri.'&'.$this->pageParam.'='.$i.'">'.$i.'';
  122. }
  123. }
  124. $F = $forStart>1?'uri.'&'.$this->pageParam.'=1">1':'';
  125. $E = $forStop<$this->pageToatl+1?'uri.'&'.$this->pageParam.'='.$this->pageToatl.'">'.$this->pageToatl.'':'';
  126. if($forStart>2){
  127. $omitF = '…';
  128. $startA = $this->page-$number<1?1:$this->page-$number;
  129. $omitFA = 'uri.'&'.$this->pageParam.'='.$startA.'">…';
  130. }
  131. if($forStop<$this->pageToatl){
  132. $omitE = '…';
  133. $stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
  134. $omitEA = 'uri.'&'.$this->pageParam.'='.$stopA.'">…';
  135. }
  136. }
  137. $this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
  138. }

  139. /**

  140. *處理url的方法
  141. * @access public
  142. * @param array $url 保持URL直對關係數組
  143. * @return string 過濾後的url尾參數
  144. */
  145. private function uri(){
  146. $url = $_SERVER["REQUEST_URI"];
  147. $par = parse_url($url);
  148. if (isset($par['query'])) {
  149. parse_str($par['query'],$query);
  150. if(!is_array($this->uri)){
  151. $this->uri = array();
  152. }
  153. array_merge($query,$this->uri);
  154. unset($query[$this->pageParam]);
  155. while($key = array_search('',$query)){
  156. unset($query[$key]);
  157. }
  158. $this->uri = $par['path'].'?'.http_build_query($query);
  159. }else{
  160. $this->uri = $par['path'].'?';
  161. }
  162. }

  163. /**

  164. * 設定limit方法及計算起始條數和結束條數
  165. */
  166. private function calculate(){
  167. $this->pageToatl = ceil($this->total/$this->pageSize);
  168. $this->page = intval($_GET[$this->pageParam]);
  169. $this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
  170. $this->pageStart = ($this->page-1)*$this->pageSize;
  171. $this->pageStop = $this->pageStart+$this->pageSize;
  172. $this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
  173. $this->limit = $this->pageStart.','.$this->pageStop;
  174. }

  175. /**

  176. * 設定過濾器
  177. */
  178. public function __set($name,$value){
  179. switch($name){
  180. case 'pageType':
  181. case 'uri':
  182. $this->$name = $value;
  183. return;
  184. case 'pageShow':
  185. if(is_array($value)){
  186. $this->pageShow = array_merge($this->pageShow,$value);
  187. }
  188. return;
  189. }
  190. }

  191. /**

  192. * 取值過濾器
  193. */
  194. public function __get($name){
  195. switch($name){
  196. case 'limit':
  197. case 'pageStart':
  198. case 'pageStop':
  199. return $this->$name;
  200. default:
  201. return;
  202. }
  203. }
  204. }

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.