搜尋
首頁後端開發php教程php分頁原理實例解析,php oop風格分頁程式碼

  1. // 建立資料庫連線

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

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
  53. $this ->isFirstPage = false;
  54. $this->isLastPage = true;
  55. break;
  56. default:
  57. $this->isFirstPage = false;
  58. $this->is>is>is; 🎜> }
  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. {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. return false;
  119. }
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. function _setOptions($option)
  127. {
  128. $allow_options = array( '
  129. $allow_options = array( ' > 'CurrentPageID',
  130. 'sql',
  131. 'numItems'
  132. );
  133. foreach ( $option as $key => $value )
  134. {
  135. if ( in_array($key, $allow_options) && ($value != null) )
  136. {
  137. $this->$key = $value;
  138. }
  139. }
  140. return true ;
  141. }
  142. }
?>
複製程式碼

2,測試php分頁程式碼

  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) $data = $pager->getPageData();
  23. if ( $pager->isFirstPage )
  24. {
  25. $turnover = "首頁|上一頁|";
  26. }
  27. $turnover = "首頁|上一頁|";
  28. }
  29. else
  30. {
  31. $turnover = "首頁|PreviousPageID."&numItems=".$pager->numItems."'>上一頁|";
  32. }
  33. if ( $pager->isLastPage )
  34. {
  35. $turnover .= "下一頁|尾頁";
  36. }
  37. else
  38. {
  39. $turnover .= " NextPageID."&numItems=".$pager->numItems."'>下一頁|numPages."&numItems=".$pager-> numItems."'>尾頁";
}
?>
複製程式碼

說明: 這個類別只是處理數據,並不負責處理顯示,因為將數據的處理和結果的顯示都放到一個類別裡邊實在是有些勉強。 顯示情況和要求多變,不如自己根據類別給出的結果處理,根據這個Pager類別繼承一個自己的子類別來顯示不同的分頁,例如顯示使用者分頁列表可以:
  1. Class MemberPager extends Pager
  2. {
  3. function showMemberList()
  4. {
  5. $data = $this->getPageData();
  6. // 顯示結果的程式碼
  7. // ......
  8. }
  9. }
  10. // / 呼叫
  11. if ( isset($_GET['page']) )
  12. {
  13. $page = (int)$_GET['page'];
  14. }
  15. else
  16. {
  17. $page = 1;
  18. }
  19. $sql = "select * from members order by id";
  20. $pager_option = array(
  21. "sql" => $sql,
  22. "PageSize" => 10,
  23. "CurrentPageID" => $page
  24. );
  25. if ( isset($_GET['numItems']) )
  26. {
  27. $pager_option[' numItems'] = (int)$_GET['numItems'];
  28. }
  29. $pager = @new MemberPager($pager_option);
  30. $pager->showMemberList();
?>
複製程式碼

第二個不同資料庫的相容性,在不同的資料庫裡截獲一段結果的寫法是不一樣的。

mysql: select * from table limit offset, rows pgsql: select * from table limit m offset n .....

在類別裡邊取得結果時需要使用pear db類別的limitQuery方法。


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
簡單地說明PHP會話的概念。簡單地說明PHP會話的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

您如何循環中存儲在PHP會話中的所有值?您如何循環中存儲在PHP會話中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

說明如何使用會話進行用戶身份驗證。說明如何使用會話進行用戶身份驗證。Apr 26, 2025 am 12:04 AM

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

舉一個如何在PHP會話中存儲用戶名的示例。舉一個如何在PHP會話中存儲用戶名的示例。Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

哪些常見問題會導致PHP會話失敗?哪些常見問題會導致PHP會話失敗?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

您如何在PHP中調試與會話相關的問題?您如何在PHP中調試與會話相關的問題?Apr 25, 2025 am 12:12 AM

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

如果session_start()被多次調用會發生什麼?如果session_start()被多次調用會發生什麼?Apr 25, 2025 am 12:06 AM

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

您如何在PHP中配置會話壽命?您如何在PHP中配置會話壽命?Apr 25, 2025 am 12:05 AM

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具