有關php分頁顯示的製作方法

WBOY
發布: 2016-07-25 09:03:45
原創
2268 人瀏覽過
  1. //建立資料庫連線

  2. $link = mysql_connect("localhost", "mysql_user", "mysql_passWord ")
  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 = my_fetch_row(sql);
  48. while ( $row = my_fetch_row(sql_fetch_row( $result) ){
  49. $rowset[] = $row;
  50. }
  51. }else{
  52. $rowset = array();
  53. }
  54. // 沒有包含顯示結果的程式碼,那不在討論範圍,只要用foreach就可以很簡單的用得到的二維數組來顯示結果
  55. ?>
複製程式碼

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;
  45. $this->isLastPage = true;
  46. break;
  47. case 1:

  48. $this- >isFirstPage = true;
  49. $this->isLastPage = false;
  50. break;
  51. case $this->numPages:

  52. $this->isFirstPage = false;
  53. $this->isLastPage = true;
  54. break;
  55. default:

  56. $this->isFirstPage = false;
  57. $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. *如果結果集不是很大,可以直接使用getPageData的方式取得二維數組格式的結果
  70. * getPageData方法也是呼叫此方法來取得結果的
  71. *
  72. ***/
  73. function getDataLink()
  74. {
  75. if ( $this->numItems )
  76. {
  77. global $db;
  78. $PageID = $this->CurrentPageID;
  79. $from = ($PageID - 1)*$this->PageSize;
  80. $count = $this->PageSize;
  81. $link = $db- >limitQuery($this->sql, $from, $count); //使用Pear DB::limitQuery方法保證資料庫相容性
  82. return $link;
  83. }
  84. else
  85. {
  86. return false;
  87. }
  88. }
  89. /***

  90. *
  91. * 以二維數組的格式傳回結果集
  92. *
  93. ***/
  94. function getPageData()
  95. {
  96. if ( $this-> numItems )
  97. {
  98. if ( $res = $this->getDataLink() )
  99. {
  100. if ( $res->numRows() )
  101. {
  102. while ( $row = $res->fetchRow() )
  103. {
  104. $result[] = $row;
  105. }
  106. }
  107. else
  108. {
  109. $result = array();
  110. }
  111. return $result;
  112. }
  113. else
  114. {
  115. return false;
  116. }
  117. }
  118. else
  119. {
  120. {}
  121. }
  122. function _setOptions($option)

  123. {
  124. $allow_options = array(
  125. 'PageSize',
  126. 'CurrentPageID' ,
  127. 'sql',
  128. 'numItems'
  129. );
  130. foreach ( $option as $key => $value )

  131. {
  132. if ( in_array($key, $allow_options) && ($value != null) )
  133. {
  134. $this->$key = $value;
  135. }
  136. }
  137. return true;
  138. }
  139. }
  140. return true;
  141. }
  142. }
?>
複製程式碼

#------------------------

  1. // FileName: test_pager.php

  2. // 這是一段簡單的範例程式碼,前邊省略了使用pear db類別建立資料庫連線的程式碼
  3. require "Pager.class.php";
  4. if ( isset($_GET['page']) )

  5. {
  6. $page = (int)$_GET['page'];
  7. }
  8. else
  9. {
  10. $page = 1;
  11. }
  12. $sql = "select * from table order by id";
  13. $pager_option = array(
  14. "sql" => $sql,
  15. "PageSize" => 10,
  16. "CurrentPageID" => $page
  17. );
  18. if ( isset($_GET['numItems']) )

  19. {
  20. $pager_option['numItems'] = (int) $_GET['numItems'];
  21. }
  22. $pager = @new Pager($pager_option);

  23. $data = $pager->getPageData();
  24. if ( $pager->isFirstPage )

  25. {
  26. $turnover = "首頁|上一頁|";
  27. }
  28. else
  29. {
  30. $ turnover = "首頁|上一頁|";
  31. }
  32. if ( $pager->isLastPage )

  33. {
  34. $ turnover .= "下一頁|尾頁";
  35. }
  36. else
  37. {
  38. $turnover .="下一頁|尾頁";
  39. }
  40. ?>
複製程式碼

說明: 這個類別只是處理數據,並不負責處理顯示,因為我覺得將數據的處理和結果的顯示都放到一個類別裡邊實在是有些勉強。顯示的時候情況和要求多變,不如自己根據類別給出的結果處理,更好的方法是根據這個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. } p>
  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 = @ 。第二個需要說明的地方就是不同資料庫的兼容性,在不同的資料庫裡截獲一段結果的寫法是不一樣的。 mysql: select * from table limit offset, rows pgsql: select * from table limit m offset n ....
  32. 所以,要在類別裡邊取得結果時,需要使用pear db類別的limitQuery方法。

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