php分頁原理簡單分頁代碼總結

來源:互聯網
上載者:User
  1. //定每頁顯示多少條

  2. //count(*)查詢總條數
  3. //計算總頁數ceil()
  4. //判斷$_GET 並賦值當前頁數
  5. //mysql查詢 limit (當前頁數-1)*每頁條數,每頁條數
  6. //如果總條數不為空白,進行迴圈mysql_fetch_array()
  7. //$_SERVER['PHP_SELF']
  8. //上一頁?page=當前頁-1
  9. //下一頁?page=當前頁+1
  10. //edit: bbs.it-home.org
  11. include("conn.php");
  12. $perpagenum = 3;
  13. $total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM message"));
  14. $Total = $total[0];
  15. $Totalpage =ceil($Total/$perpagenum);

  16. if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage){

  17. $page = 1;
  18. }else{
  19. $page = $_GET['page'];
  20. }

  21. $start = ($page-1)*$perpagenum;

  22. $sql = "SELECT * FROM message ORDER BY id DESC LIMIT $start,$perpagenum";
  23. $result = mysql_query($sql);
  24. $contents = mysql_fetch_array($result);
  25. ?>
  26. if($total){
  27. }
  28. do{
  29. $user = $contents['user'];
  30. $contact = $contents['contact'];
  31. $content = $contents['content'];
  32. $date = $contents['date'];

  33. ?>

  34. //php分頁開始
  35. while($contents=mysql_fetch_array($result));
  36. $per = $page-1;
  37. $next= $page+1;
  38. echo "共有".$Total."條留言,每頁".$perpagenum."條,共".$page."/".$Totalpage."頁。";
  39. if($page!=1){
  40. echo "首頁".
  41. "上一頁";
  42. }
  43. if($page!=$Totalpage){
  44. echo "下一頁";
  45. echo "尾頁";
  46. }
  47. ?>
  48. }
  49. ?>
  50. 暱稱: 發表於:

複製代碼
  • 聯繫我們

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