PHP分頁程式碼詳解(附實例)

WBOY
發布: 2016-07-25 08:52:24
原創
2420 人瀏覽過
  1. // 建立資料庫連線

  2. $link = mysql_connect("localhost", "mysql_user", "mysql_Word ")
  3. or die("Could not connect: " . mysql_error());
  4. // 取得目前頁數
  5. if( isset($_GET['page']) ){
  6. $ page = intval( $_GET['page'] );
  7. }
  8. else{
  9. $page = 1;
  10. }
  11. // 每頁數量
  12. $PageSize = 10;
  13. // 取得總資料量
  14. $sql = "select count(*) as amount from table";
  15. $result = mysql_query($sql);
  16. $row = mysql_fetch_row($result) ;
  17. $amount = $row['amount'];
  18. // 記算總共有多少頁
  19. if( $amount ){
  20. if( $amount if( $amount % $page_size ){ //取總資料量除以每頁數的餘數
  21. $page_count = (int )($amount / $page_size) 1; //如果有餘數,則頁數等於總資料量除以每頁數的結果取整數再加一
  22. }else{
  23. $page_count = $amount / $page_size; //如果沒有餘數,則頁數等於總資料量除以每頁數的結果
  24. }
  25. }
  26. else{
  27. $page_count = 0;
  28. }
  29. // 翻頁連結
  30. $page_string = '';
  31. if( $page == 1 ){
  32. $page_string .= '第一頁|上一頁|';
  33. }
  34. else{
  35. $page_string .= '第一頁|上一頁|';
  36. }
  37. if( ($page == $page_count) || ($page_count == 0) ){
  38. $page_string .= '下一頁|尾頁' ;
  39. }
  40. else{
  41. $page_string .= '下一頁|尾頁';
  42. }
  43. // 取得數據,以二維數組格式傳回結果
  44. if( $amount ){
  45. $sql = " select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
  46. $result = mysql_query($sql);
  47. while ( $row = mysql_fetch_row($result) ){

  48. $rowset[] = $row;
  49. }
  50. }else{
  51. $rowset = array();
  52. }
  53. / / 沒有包含顯示結果的程式碼,那不在討論範圍,只要用foreach就可以很簡單的用得到的二維數組來顯示結果
  54. ?>
複製程式碼

4、OO風格程式碼 資料庫連線使用pear db類別進行處理。

  1. // FileName: Pager.class.php

  2. // 分頁類,這類僅用於處理資料結構,不負責處理顯示
  3. Class Pager
  4. {
  5. var $PageSize; //每頁的數量
  6. var $CurrentPageID; //目前的頁數
  7. var $NextPageID; / /下一頁
  8. var $PreviousPageID; //上一頁
  9. var $numPages; //總頁數
  10. var $numItems; //總記錄數
  11. var $isFirstPage; //是否第一頁
  12. var $isLastPage; //是否最後一頁
  13. var $sql; //sql查詢語句
  14. function Pager($option)

  15. {
  16. global $db;
  17. $this->_setOptions($option);
  18. // 總條數
  19. if ( !isset($this->numItems) )
  20. {
  21. $res = $db->query($this->sql);
  22. $this->numItems = $res->numRows();
  23. }
  24. // 總頁數
  25. if ( $this ->numItems > 0 )
  26. {
  27. if ( $this->numItems PageSize ){ $this->numPages = 1; }
  28. if ( $this->numItems % $this ->PageSize )
  29. {
  30. $this->numPages= (int)($this->numItems / $this->PageSize) 1;
  31. }
  32. else
  33. {
  34. $this->numPages = $this->numItems / $this->PageSize;
  35. }
  36. }
  37. else
  38. {
  39. $this->numPages = 0;
  40. }
  41. switch ( $this->CurrentPageID )

  42. {
  43. case $this->numPages == 1:
  44. $this->isFirstPage = true;isLastPage = true;
  45. break;
  46. case 1:
  47. $this->isFirstPage = true;
  48. $this->isLastPage = false;
  49. break;
  50. case
  51. this $ ->numPages:
  52. $this->isFirstPage = false;
  53. $this->isLastPage = true;
  54. break;
  55. default:
  56. $this->isFirstPage = 🎜> default:
  57. $this->isFirstPage = false; $this->isLastPage = false;
  58. }
  59. if ( $this->numPages > 1 )

  60. {
  61. if ( !$this->isLastPage ) { $ this->NextPageID = $this->CurrentPageID 1; }
  62. if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; }
  63. }
  64. return true;

  65. }
  66. /***

  67. *
  68. * 返回結果集的資料庫連接
  69. * 在結果集比較大的時候可以直接使用這個方法獲得資料庫連接,然後在類別之外遍歷,這樣開銷較小
  70. *如果結果集不是很大,可以直接使用getPageData的方式取得二維陣列格式的結果
  71. * getPageData方法也是呼叫此方法來取得結果的
  72. *
  73. ***/
  74. function getDataLink()

  75. {
  76. if ( $this->numItems )
  77. {
  78. global $db;
  79. $PageID = $this->CurrentPageID;

  80. $ from = ($PageID - 1)*$this->PageSize;

  81. $count = $this->PageSize;
  82. $link = $db->limitQuery($this->sql, $from, $count ); //使用Pear DB::limitQuery方法保證資料庫相容性
  83. return $link;

  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90. /***

  91. *
  92. * 以二維數組的格式傳回結果集
  93. *
  94. ***/
  95. function getPageData()

  96. {
  97. if ( $this-> numItems )
  98. {
  99. if ( $res = $this->getDataLink() )
  100. {
  101. if ( $res->numRows() )
  102. {
  103. while ( $row = $res->fetchRow() )
  104. {
  105. $result[] = $row;
  106. }
  107. }
  108. else
  109. {
  110. $result = array();
  111. }
  112. return $result;

  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. else }
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. function _setOptions($option)

  127. {
  128. $allow_options = array( 'PageSize' ,
  129. 'CurrentPageID',
  130. 'sql',
  131. 'numItems'
  132. );
  133. foreach ( $option as $key => $value )

  134. foreach ( $option as $key => $value )

  135. {
  136. if ( in_array($key, $allow_options) && ($value != null) )
  137. {
  138. $this->$key = $value;
  139. } }

    return true; }

  140. }
  141. ?>
  142. 呼叫範例:

  143. // FileName: test_pager.php
  144. // 省略了使用pear db類別建立資料庫連接的程式碼
  145. require "Pager.class.php";
  146. if ( isset($_GET['page']) )
  147. {
  148. $page = (int)$_GET['page '];
  149. }
  150. else
  151. {
  152. $page = 1;
  153. }
  154. $sql = "select * from table order by id";
  155. $pager_option = array (
  156. "sql" => $sql,
  157. "PageSize" => 10,
  158. "CurrentPageID" => $page
  159. );
  160. if ( isset($_GET['numItems' ]) )
  161. {
  162. $pager_option['numItems'] = (int)$_GET['numItems'];
  163. }
  164. $pager = @new Pager($pager_option);
  165. $data = $pager->getPageData();
  166. if ( $pager->isFirstPage )
  167. {
  168. $turnover = "首頁|上一頁|";
  169. }
  170. else
  171. {
  172. $turnover = "首頁|上一頁|";
  173. }
  174. if ( $pager->isLastPage )
  175. {
  176. $ turnover .= "下一頁|尾頁";
  177. }
  178. else
  179. {
  180. $turnover .= "下一頁|尾頁";
  181. }
  182. ?>
複製程式碼

說明兩點: 這個類別只是處理數據,並不負責處理顯示,因為我覺得將數據的處理和結果的顯示都放到一個類別裡邊實在是有些勉強。 顯示的時候情況和要求多變,不如自己根據類別給出的結果處理,更好的方法是根據這個Pager類別繼承一個自己的子類別來顯示不同的分頁,例如顯示使用者分頁列表:

  1. Class MemberPager extends Pager

  2. {
  3. function showPager extends Pager
  4. {
  5. function showPager()
  6. global $db;
  7. $data = $this->getPageData();

  8. // 顯示結果的程式碼
  9. // ......
  10. }
  11. }
  12. /// 呼叫
  13. if ( isset($_GET['page']) )
  14. {
  15. $page = (int)$_GET['page'];
  16. }
  17. else
  18. {
  19. $page = 1;
  20. }
  21. $sql = "select * from members order by id";
  22. $pager_option = array(
  23. "sql" => $sql,
  24. "PageSize" => 10,
  25. "CurrentPageID" => $page
  26. );
  27. if ( isset($_GET['numItems']) )
  28. {
  29. $pager_option['numItems'] = (int)$_GET['numItems'];
  30. }
  31. $pager = @new MemberPager($pager_option);
  32. $pager-> showMemberList();
  33. ?>
複製程式碼

說明:不同資料庫的相容性,在不同的資料庫中截獲一段結果的寫法是不一樣的。 mysql: select * from table limit offset, rows pgsql: select * from table limit m offset n ..... 因此,在類別裡邊取得結果時,需要使用pear db類別的limitQuery方法。


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!