Php paging principle instance parsing, php simple paging code

Source: Internet
Author: User
Php paging principle instance parsing, php simple paging code

  1. $ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1.
  2. $ Num = 10; // 10 data entries per page
  3. $ Db = mysql_connect ("host", "name", "pass"); // create a database connection
  4. $ Select = mysql_select_db ("db", $ db); // select the database to operate
  5. $ Total = mysql_num_rows (mysql_query ("select * from table"); // The total number of queried data
  6. $ Pagenum = ceil ($ total/$ num); // obtain the total number of pages
  7. // If the input page number parameter is greater than the total page number, the error message is displayed.
  8. If ($ page> $ pagenum | $ page = 0 ){
  9. Echo "Error: Can Not Found The page .";
  10. Exit;
  11. }
  12. $ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.
  13. $ Info = mysql_query ("select * from table limit $ offset, $ num"); // obtain the data to be displayed for the corresponding page number.
  14. While ($ it = mysql_fetch_array ($ info )){
  15. Echo $ it ['name']."
    ";
  16. For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
  17. $ Show = ($ I! = $ Page )? "$ I ":"$ I";
  18. Echo $ show ."";
  19. }
  20. }
  21. ?>

You can use the above code to replace the database connection and query tables with your own, and test the paging effect.

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.