php分頁代碼執行個體有注釋

來源:互聯網
上載者:User
  1. header("content-type:text/html;charset=utf-8");
  2. //資料庫連接
  3. $conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());
  4. mysql_select_db("test", $conn);
  5. mysql_query("set names utf8");
  6. //查詢共有多少行資料
  7. $sql1 = "select count(*) from user";
  8. $ret1 = mysql_query($sql1);
  9. $row1 = mysql_fetch_row($ret1);
  10. $tot = $row1[0];
  11. //每頁多少行資料
  12. $length = 5;
  13. //總頁數
  14. $totpage = ceil($tot / $length);
  15. //當前頁數
  16. $page = @$_GET['p'] ? $_GET['p'] : 1;
  17. //limit 下限
  18. $offset = ($page - 1) * $length;
  19. echo "";
  20. echo "

    php padding

    ";
  21. echo "
  22. echo "
  23. echo "
  24. echo "
  25. echo "
  26. echo "
  27. //將查詢出來的資料用表格顯示
  28. echo "
  29. echo "
  30. }
  31. "; "; "; "; "; ";
  32. $sql2 = "select * from user order by id limit {$offset}, {$length}";
  33. $ret2 = mysql_query($sql2);
  34. while ($row2 = mysql_fetch_assoc($ret2)) {
  35. echo "
  36. "; "; ";
  37. echo "
  38. IDUSERPASS
    {$row2['id']} {$row2['name']} {$row2['pass']}
    ";
  39. //上一頁和下一頁
  40. $prevpage = $page - 1;
  41. if ($page >= $totpage) {
  42. $nextpage = $totpage;
  43. } else {
  44. $nextpage = $page + 1;
  45. }
  46. //跳轉
  47. echo "

    上一頁|下一頁

    ";
  48. echo "
  49. ";
複製代碼

分頁代碼關鍵點:<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和頁數之間的關係。<2>上一頁和下一頁的獲得方式,以及臨界點。

  • 聯繫我們

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