当前位置:Gxlcms > PHP教程 > php分页代码实例有注释

php分页代码实例有注释

时间:2021-07-01 10:21:17 帮助过:5人阅读

  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. ";
  24. echo "
  25. ";
  26. echo "
  27. ";
  28. echo "
  29. ";
  30. echo "
  31. ";
  32. //将查询出来的数据用表格显示
  33. $sql2 = "select * from user order by id limit {$offset}, {$length}";
  34. $ret2 = mysql_query($sql2);
  35. while ($row2 = mysql_fetch_assoc($ret2)) {
  36. echo "
  37. ";
  38. echo "
  39. ";
  40. echo "
  41. ";
  42. }
  43. echo "
  44. IDUSERPASS
    {$row2['id']}{$row2['name']}{$row2['pass']}
    ";
  45. //上一页和下一页
  46. $prevpage = $page - 1;
  47. if ($page >= $totpage) {
  48. $nextpage = $totpage;
  49. } else {
  50. $nextpage = $page + 1;
  51. }
  52. //跳转
  53. echo "

    上一页|下一页

    ";
  54. echo "
  55. ";

分页代码关键点: <1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和页数之间的关系。 <2>上一页和下一页的获得方式,以及临界点。

人气教程排行