php分頁代碼簡單實現

來源:互聯網
上載者:User

標籤:

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

資料庫操作類代碼:mysqli.func.php

[php] view plain copy
  1. <?php  
  2. // 資料庫連接常量  
  3. define(‘DB_HOST‘, ‘localhost‘);  
  4. define(‘DB_USER‘, ‘root‘);  
  5. define(‘DB_PWD‘, ‘‘);  
  6. define(‘DB_NAME‘, ‘guest‘);  
  7.   
  8. // 串連資料庫  
  9. function conn()  
  10. {  
  11.     $conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);  
  12.     mysqli_query($conn, "set names utf8");  
  13.     return $conn;  
  14. }  
  15.   
  16. //獲得結果集  
  17. function doresult($sql){  
  18.    $result=mysqli_query(conn(), $sql);  
  19.    return  $result;  
  20. }  
  21.   
  22. //結果集轉為對象集合  
  23. function dolists($result){  
  24.     return mysqli_fetch_array($result, MYSQL_ASSOC);  
  25. }  
  26.   
  27. function totalnums($sql) {  
  28.     $result=mysqli_query(conn(), $sql);  
  29.     return $result->num_rows;  
  30. }  
  31.   
  32.   
  33.   
  34.   
  35. // 關閉資料庫  
  36. function closedb()  
  37. {  
  38.     if (! mysqli_close()) {  
  39.         exit(‘關閉異常‘);  
  40.     }  
  41. }  
  42.   
  43. ?>  


分頁實現代碼:

[php] view plain copy
  1. <?php  
  2. include ‘mysqli.func.php‘;  
  3.   
  4. // 總記錄數  
  5. $sql = "SELECT dg_id  FROM  tb_user ";  
  6. $totalnums = totalnums($sql);  
  7.   
  8. // 每頁顯示條數  
  9. $fnum = 8;  
  10.   
  11. // 翻頁數  
  12. $pagenum = ceil($totalnums / $fnum);  
  13.   
  14. // 頁數常量  
  15. @$tmp = $_GET[‘page‘];  
  16.   
  17. //防止惡意翻頁  
  18. if ($tmp > $pagenum)  
  19.     echo "<script>window.location.href=‘index.php‘</script>";  
  20.   
  21. //計算分頁起始值  
  22. if ($tmp == "") {  
  23.     $num = 0;  
  24. } else {  
  25.     $num = ($tmp - 1) * $fnum;  
  26. }  
  27.   
  28. // 查詢語句  
  29. $sql = "SELECT dg_id,dg_username  FROM  tb_user ORDER BY dg_id DESC LIMIT " . $num . ",$fnum";  
  30. $result = doresult($sql);  
  31.   
  32. // 遍曆輸出  
  33. while (! ! $rows = dolists($result)) {  
  34.     echo $rows[‘dg_id‘] . " " . $rows[‘dg_username‘] . "<br>";  
  35. }  
  36.   
  37. // 翻頁連結  
  38. for ($i = 0; $i < $pagenum; $i ++) {  
  39.     echo "<a href=index.php?page=" . ($i + 1) . ">" . ($i + 1) . "</a>";  
  40. }  
  41. ?>  


簡單實現了翻頁功能,資料庫請自行配置結構

php分頁代碼簡單實現

聯繫我們

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