PHP paging code examples have comments

WBOY
Release: 2016-07-25 08:54:16
Original
952 people have browsed it
  1. header("content-type:text/html;charset=utf-8");
  2. //Database connection
  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. //Query how many rows of data there are in total
  7. $sql1 = " select count(*) from user";
  8. $ret1 = mysql_query($sql1);
  9. $row1 = mysql_fetch_row($ret1);
  10. $tot = $row1[0];
  11. //How many rows of data per page
  12. $length = 5;
  13. //Total number of pages
  14. $totpage = ceil($tot / $length);
  15. //Current number of pages
  16. $page = @$_GET['p'] ? $_GET['p'] : 1 ;
  17. //limit lower 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. //Display the queried data in a table
  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. ID USER PASS
    { $row2['id']}{$row2['name']}{$row2['pass']}
    ";
  45. //Previous page and next page
  46. $prevpage = $page - 1;
  47. if ($page >= $totpage) {
  48. $nextpage = $totpage;
  49. } else {
  50. $nextpage = $page + 1;
  51. }
  52. //Jump
  53. echo "

    Previous page|Next page

    ";
  54. echo "";
Copy code

Key points of pagination code: "$sql2 = "select * from user order by id limit {$offset}, {$length}";", the relationship between $offset, $length and the number of pages. How to get the previous page and next page, and the critical point.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!