Php paging principle example tutorial, php paging principle demonstration

Source: Internet
Author: User
Php paging principle example tutorial, php paging principle demonstration

  1. // Connect to the database
  2. $ Con = mysql_connect ("localhost", "root ","");
  3. Mysql_select_db ("xueshengchu", $ con );
  4. Mysql_query ("set names utf8 ");
  5. $ PageSize = 5; // number of data entries per page
  6. $ Result = mysql_query ("select * from stu_msg ");
  7. $ TotalNum = mysql_num_rows ($ result); // The total number of data entries.
  8. $ TotalPageCount = intval ($ totalNum/$ pageSize); // total number of pages
  9. // Determine the current page
  10. $ NowPage = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1;
  11. // Previous Page
  12. $ Prev = ($ nowPage-1 <= 0 )? 1: $ nowPage-1;
  13. // Next page
  14. $ Next = ($ nowPage + 1 >=$ totalPageCount )? $ TotalPageCount: $ nowPage + 1;
  15. // Offset
  16. $ Offset = ($ nowPage-1) * $ pageSize;
  17. /* Data can be retrieved. The paging principle of SQL statements
  18. * Select * from table limit $ offset, $ size paging execution statement
  19. */Bbs.it-home.org
  20. $ SQL = "select * from stu_msg limit $ offset, $ pageSize ";
  21. $ Result = mysql_query ($ SQL, $ con );
  22. While ($ arr = mysql_fetch_array ($ result )){
  23. Echo $ arr ['mid ']. $ arr ['content']."
    ";
  24. }
  25. Echo "homepage ";
  26. Echo "previous page ";
  27. Echo "next page ";
  28. Echo "last page ";
  29. ?>

Php paging code demo:

Code description: php code above png has been commented out clearly

Analysis:

1. the global variable $ _ SERVER ['php _ SELF '] is the absolute path to get the script variable.

2. the essence of paging is to display the data of the database in a segment. for example, in the above php instance, extract the first five (the first page ): select * from table limit 5th fetch 10th to 1 (second page): select * from table limit 5 5 get the offset formula: $ offset = ($ nowPage-1) * $ pageSize;

3. the intval () function is used to obtain the integer value of the variable.

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.