Php paging code instance with comments

Source: Internet
Author: User
Php paging code instance with comments

  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 the total number of rows of data
  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 page number
  16. $ Page = @ $ _ GET ['P']? $ _ GET ['P']: 1;
  17. // Lower limit of 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. // Display the queried data in a table
  28. Echo"
  29. Echo"
  30. }
  31. $ Sql2 = "select * from user order by id limit {$ offset}, {$ length }";
  32. $ Ret2 = mysql_query ($ sql2 );
  33. While ($ row2 = mysql_fetch_assoc ($ ret2 )){
  34. Echo"
  35. Echo"
  36. "; "; "; "; "; "; "; "; ";
    IDUSERPASS
    {$ Row2 ['id']} {$ Row2 ['name']} {$ Row2 ['pass']}
    ";
  37. // Previous and next pages
  38. $ Prevpage = $ page-1;
  39. If ($ page >=$ totpage ){
  40. $ Nextpage = $ totpage;
  41. } Else {
  42. $ Nextpage = $ page + 1;
  43. }
  44. // Jump
  45. Echo "previous page | next page ";
  46. Echo"
  47. ";

Key points of paging code: <1> "$ sql2 =" select * from user order by id limit {$ offset}, {$ length }";", the relationship between $ offset, $ length, and the number of pages. <2> obtain the previous and next pages, as well as the critical point.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.