欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php分页原理简单分页代码总结

程序员文章站 2022-05-02 08:49:03
...
  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. do{
  28. $user = $contents['user'];
  29. $contact = $contents['contact'];
  30. $content = $contents['content'];
  31. $date = $contents['date'];
  32. ?>

  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. 昵称: 发表于:
复制代码