php+mysql分頁類

來源:互聯網
上載者:User
封裝的類:
  1. /*********************************************
  2. 類名: PageSupport
  3. 功能:分頁顯示mysql資料庫中的資料
  4. ***********************************************/
  5. class PageSupport{
  6. //屬性
  7. var $sql; //所要顯示資料的SQL查詢語句
  8. var $page_size; //每頁顯示最多行數
  9. var $start_index; //所要顯示記錄的首行序號
  10. var $total_records; //記錄總數
  11. var $current_records; //本頁讀取的記錄數
  12. var $result; //讀出的結果
  13. var $total_pages; //總頁數
  14. var $current_page; //當前頁數
  15. var $display_count = 30; //顯示的前幾頁和後幾頁數
  16. var $arr_page_query; //數組,包含分頁顯示需要傳遞的參數
  17. var $first;
  18. var $prev;
  19. var $next;
  20. var $last;
  21. //方法
  22. /*********************************************
  23. 建構函式:__construct()
  24. 輸入參數:
  25. $ppage_size:每頁顯示最多行數
  26. ***********************************************/
  27. function PageSupport($ppage_size)
  28. {
  29. $this->page_size=$ppage_size;
  30. $this->start_index=0;
  31. }
  32. /*********************************************
  33. 建構函式:__destruct()
  34. 輸入參數:
  35. ***********************************************/
  36. function __destruct()
  37. {
  38. }
  39. /*********************************************
  40. get函數:__get()
  41. ***********************************************/
  42. function __get($property_name)
  43. {
  44. if(isset($this->$property_name))
  45. {
  46. return($this->$property_name);
  47. }
  48. else
  49. {
  50. return(NULL);
  51. }
  52. }
  53. /*********************************************
  54. set函數:__set()
  55. ***********************************************/
  56. function __set($property_name, $value)
  57. {
  58. $this->$property_name = $value;
  59. }
  60. /*********************************************
  61. 函數名:read_data
  62. 功能: 根據SQL查詢語句從表中讀取相應的記錄
  63. 傳回值:屬性二維數組result[記錄號][欄位名]
  64. ***********************************************/
  65. function read_data()
  66. {
  67. $psql=$this->sql;
  68. //查詢資料,資料庫連結等資訊應在類調用的外部實現
  69. $result=mysql_query($psql) or die(mysql_error());
  70. $this->total_records=mysql_num_rows($result);
  71. //利用LIMIT關鍵字擷取本頁所要顯示的記錄
  72. if($this->total_records>0)
  73. {
  74. $this->start_index = ($this->current_page-1)*$this->page_size;
  75. $psql=$psql. " LIMIT ".$this->start_index." , ".$this->page_size;
  76. $result=mysql_query($psql) or die(mysql_error());
  77. $this->current_records=mysql_num_rows($result);
  78. //將查詢結果放在result數組中
  79. $i=0;
  80. while($row=mysql_fetch_Array($result))
  81. {
  82. $this->result[$i]=$row;
  83. $i++;
  84. }
  85. }
  86. //擷取總頁數、當前頁資訊
  87. $this->total_pages=ceil($this->total_records/$this->page_size);
  88. $this->first=1;
  89. $this->prev=$this->current_page-1;
  90. $this->next=$this->current_page+1;
  91. $this->last=$this->total_pages;
  92. }
  93. /*********************************************
  94. 函數名:standard_navigate()
  95. 功能: 顯示首頁、下頁、上頁、未頁
  96. ***********************************************/
  97. function standard_navigate()
  98. {
  99. echo "";
  100. echo "";
  101. echo "";
  102. }
  103. /*********************************************
  104. 函數名:full_navigate()
  105. 功能: 顯示首頁、下頁、上頁、未頁
  106. 產生導航連結 如1 2 3 ... 10 11
  107. ***********************************************/
  108. function full_navigate()
  109. {
  110. echo "";
  111. echo "";
  112. echo "";
  113. }
  114. }
  115. ?>
複製代碼
寫在php頁面裡面的代碼:
  1. include_once("fenye_php.php"); //引入類
  2. ///////////////////////////////////////////////////////////////////////
  3. $con = mysql_connect("localhost","root","");
  4. if (!$con)
  5. {
  6. die('Could not connect: ' . mysql_error());
  7. }
  8. mysql_select_db("myblog", $con); //選取資料庫
  9. $PAGE_SIZE=10; //設定每頁顯示的數目
  10. ///////////////////////////////////////////////////////////////////////
  11. $pageSupport = new PageSupport($PAGE_SIZE); //執行個體化PageSupport對象
  12. $current_page=$_GET["current_page"];//分頁當前頁數
  13. if (isset($current_page)) {
  14. $pageSupport->__set("current_page",$current_page);
  15. } else {
  16. $pageSupport->__set("current_page",1);
  17. }
  18. $pageSupport->__set("sql","select * from article ");
  19. $pageSupport->read_data();//讀資料
  20. if ($pageSupport->current_records > 0) //如果資料不為空白,則組裝資料
  21. {
  22. for ($i=0; $i<$pageSupport->current_records; $i++)
  23. {
  24. $title = $pageSupport->result[$i]["title"];
  25. $content = $pageSupport->result[$i]["content"];
  26. $part=substr($content,0,400);
  27. //迴圈輸出每條資料
  28. echo '
  29. '.$title.'
  30. '.$part.'
  31. update delet
  32. ';
  33. }
  34. }
  35. $pageSupport->standard_navigate(); //調用類裡面的這個函數,顯示出分頁HTML
  36. //關閉資料庫
  37. mysql_close($con);
  38. ?>
複製代碼
來自:http://blog.csdn.net/phpfenghuo/article/details/23207099
分頁, php, mysql
  • 聯繫我們

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