一個php分頁類代碼(相容url路由)

來源:互聯網
上載者:User
  1. class Page{
  2. private $total; //資料表中總記錄數
  3. private $listRows; //每頁顯示的條數
  4. private $limit; //SQL語句中使用limit從句,限制擷取記錄數
  5. private $uri; //自動擷取url的請求地址
  6. private $pageNum; //總頁數
  7. private $page; //當前頁
  8. private $config = array(
  9. 'head' => '條記錄',
  10. 'prev' => '上一頁',
  11. 'next' => '下一頁',
  12. 'first' => '首頁',
  13. 'last' => '末頁'
  14. );//在分頁資訊中顯示內容,可以自己通過set()方法設定
  15. private $listNum = 5; //預設分頁列表顯示的個數
  16. /**
  17. * 構造方法,可以設定分頁類的屬性
  18. * @param int $total計算分頁的總記錄數
  19. * @param int $listRows 可選的,設定每頁顯示的記錄數,預設25
  20. * @param mixed $query可選的,為向目標頁面傳遞參數,可以是數組,也可以是查詢字串格式
  21. * @param bool $ord 預設是true,頁面從第一頁開始顯示,false則從最後一頁開始顯示
  22. */ bbs.it-home.org
  23. public function __construct($total, $listRows=25, $query='', $ord=true){
  24. $this->total = $total;
  25. $this->listRows = $listRows;
  26. $this->uri = $this->getUri($query);
  27. $this->pageNum = ceil($this->total/$this->listRows);
  28. /*以下判斷用來設定當前頁*/
  29. if(!empty($_GET['page'])){
  30. $page = $_GET['page'];
  31. }else{
  32. if($ord){
  33. $page = 1;
  34. }else{
  35. $page = $this->pageNum;
  36. }
  37. }
  38. if($total > 0){
  39. if(preg_match('/\D/', $page)){
  40. $this->page = 1;
  41. }else{
  42. $this->page = $page;
  43. }
  44. }
  45. //$this->limit = 'LIMIT '.$this->setLimit();
  46. $this->limit = $this->setLimit();
  47. }
  48. /**
  49. * 用於設定顯示分頁的資訊,可以進行連貫操作
  50. * @param string $param 成員屬性數組config的下表
  51. * @param string $value 用於設定config下標對應的元素值
  52. * @return object 返回本對象自己$this,用於連貫操作
  53. */
  54. function __set($param, $value){
  55. if(array_key_exists($param, $this->config)){
  56. $this->config[$param] = $value;
  57. }
  58. //return $this;
  59. }
  60. /*可以使用在對象外部,直接擷取私人成員屬性limit和page的值*/
  61. function __get($args){
  62. if($args == 'limit' || $args == 'page'){
  63. return $this->$args;
  64. }else{
  65. return null;
  66. }
  67. }
  68. /**
  69. * 按指定的格式輸出分頁
  70. * @param int 0-7的數字分別作為參數,用於自訂輸出分頁結構和調整結構的順序,預設輸出全部結構
  71. * @return string 分頁資訊內容
  72. */
  73. function fpage(){
  74. $arr = func_get_args();
  75. $html[0] = " 共 {$this->total} {$this->config['head']} ";
  76. $html[1] = " 本頁".$this->disnum()."條 ";
  77. $html[2] = " 本頁從{$this->start()} - {$this->end()}條 ";
  78. $html[3] = " {$this->page}/{$this->pageNum}頁 ";
  79. $html[4] = $this->firstprev();
  80. $html[5] = $this->pageList();
  81. $html[6] = $this->nextlast();
  82. $html[7] = $this->goPage();
  83. $fpage = '';
  84. if(count($arr)<1){
  85. $arr = array(0,1,2,3,4,5,6,7);
  86. }
  87. for($i=0; $i$fpage .= $html[$arr[$i]];
  88. }
  89. $fpage .= '';
  90. return $fpage;
  91. }
  92. /*在對象內部使用,用於自動擷取訪問的當前url*/
  93. private function getUri($query){
  94. $request_uri = $_SERVER['REQUEST_URI'];
  95. $url = strstr($request_uri, '?') ? $request_uri : $request_uri.'?';
  96. if(is_array($query)){
  97. $url .= http_build_query($query);
  98. }else if($query != ''){
  99. $url .= '&'.trim($query, '?&');
  100. }
  101. $arr = parse_url($url);
  102. if(isset($arr['query'])){
  103. parse_str($arr['query'], $arrs);
  104. unset($arrs['page']);
  105. $url = $arr['path'].'?'.http_build_query($arrs);
  106. }
  107. if(strstr($url, '?')){
  108. if(substr($url, -1) != '?'){
  109. $url = $url.'&';
  110. }
  111. }else{
  112. $url .= '?';
  113. }
  114. return $url;
  115. }
  116. /*私人方法,設定limit*/
  117. private function setLimit(){
  118. if($this->page > 0){
  119. return ($this->page - 1)*$this->listRows.", {$this->listRows}";
  120. }else{
  121. return 0;
  122. }
  123. }
  124. /*在對象內部使用的私人方法,用於擷取當前頁開始的記錄數*/
  125. private function start(){
  126. if($this->total == 0){
  127. return 0;
  128. }else{
  129. return ($this->page-1) * $this->listRows + 1;
  130. }
  131. }
  132. /*用於擷取當前頁結束的記錄數*/
  133. private function end(){
  134. return min($this->page * $this->listRows, $this->total);
  135. }
  136. /*用於擷取本頁顯示的記錄條數*/
  137. private function disnum(){
  138. if($this->total > 0){
  139. return $this->end() - $this->start() + 1;
  140. }else{
  141. return 0;
  142. }
  143. }
  144. /*用於擷取上一頁和首頁的操作資訊*/
  145. private function firstprev(){
  146. if($this->page > 1){
  147. $str = " uri}page=1'>{$this->config['first']} ";
  148. $str .= "uri}page=".($this->page-1)."'>{$this->config['prev']} ";
  149. return $str;
  150. }
  151. }
  152. private function pageList(){
  153. $linkPage = ' ';
  154. $pageSub = $this->page%$this->listNum;
  155. if($pageSub==0 && $this->page>0){
  156. $pageSub = $this->listNum;
  157. }
  158. /*當前頁面前面的列表*/
  159. for($i=$pageSub-1; $i >= 1; $i--){
  160. $page = $this->page-$i;
  161. if($page >= 1){
  162. $linkPage .= "uri}page={$page}'>{$page} ";
  163. }
  164. }
  165. /*當前頁的資訊*/
  166. if($this->pageNum > 1){
  167. $linkPage .= "{$this->page} ";
  168. }
  169. /*當前頁後面的列表*/
  170. for($i=1; $i<=$this->listNum-$pageSub; $i++){
  171. $page = $this->page + $i;
  172. if($page<=$this->pageNum){
  173. $linkPage .= "uri}page={$page}'>{$page} ";
  174. }else{
  175. break;
  176. }
  177. }
  178. $linkPage .= '';
  179. return $linkPage;
  180. }
  181. /*用於擷取頁數列表資訊*/
  182. private function pageListBak(){
  183. $linkPage = ' ';
  184. $inum = floor($this->listNum/2);
  185. /*當前頁面前面的列表*/
  186. for($i=$inum; $i >= 1; $i--){
  187. $page = $this->page-$i;
  188. if($page >= 1){
  189. $linkPage .= "uri}page={$page}'>{$page} ";
  190. }
  191. }
  192. /*當前頁的資訊*/
  193. if($this->pageNum > 1){
  194. $linkPage .= "{$this->page} ";
  195. }
  196. /*當前頁後面的列表*/
  197. for($i=1; $i<=$inum; $i++){
  198. $page = $this->page + $i;
  199. if($page<=$this->pageNum){
  200. $linkPage .= "uri}page={$page}'>{$page} ";
  201. }else{
  202. break;
  203. }
  204. }
  205. $linkPage .= '';
  206. return $linkPage;
  207. }
  208. /*擷取下一頁和尾頁的操作資訊*/
  209. private function nextlast(){
  210. if($this->page != $this->pageNum){
  211. $str = " uri}page=".($this->page+1)."'>{$this->config['next']} ";
  212. $str .= " uri}page={$this->pageNum}'>{$this->config['last']} ";
  213. return $str;
  214. }
  215. }
  216. /*用於顯示和處理表單跳轉頁面*/
  217. private function goPage(){
  218. if($this->pageNum > 1){
  219. return ' ';
  220. }
  221. }
  222. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.