Php + mysql paging query code and demo

Source: Internet
Author: User
Php + mysql paging query code and demo

  1. // To avoid errors caused by repeated file inclusion, the following conditions are added to determine whether a function exists:

  2. If (! Function_exists (pages )){
  3. // Defines the function pages (). The meaning of the three parameters is:
  4. // $ Total: total information;
  5. // $ Displaypg: number of information displayed on each page. the default value is 20;
  6. // $ Url: the link in the paging navigation. except for adding different query information "page", the link is the same as the URL.
  7. // The default value should be the URL of the current page (that is, $ _ SERVER ["REQUEST_URI"]), but the default value can only be a constant on the right, so this default value is set to an empty string, set this page URL in the function.
  8. Function pages ($ total, $ displaypg = 20, $ url = ''){

  9. // Define several global variables:

  10. // $ Page: current page number;
  11. // $ Firstcount: (database) The start item of the query;
  12. // $ Pagenav: page navigation bar code, which is not output in the function;
  13. // $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page.
  14. Global $ page, $ firstcount, $ pagenav, $ _ SERVER;

  15. // To enable external function access to "$ displaypg", set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so the value is assigned again here.

  16. $ GLOBALS ["displaypg"] = $ displaypg;
  17. $ Page = $ _ GET ['Page'];
  18. If (! $ Page) $ page = 1;

  19. // If $ url uses the default value, that is, null value, the value is assigned to the URL of the current page:

  20. If (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"];}

  21. // URL analysis:

  22. $ Parse_url = parse_url ($ url );
  23. $ Url_query = $ parse_url ["query"]; // Retrieve the query string of the URL separately.
  24. If ($ url_query ){
  25. // Because the URL may contain page number information, remove it to add new page number information.
  26. // Regular expression
  27. $ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );

  28. // Replace the query string of the processed URL with the query string of the original URL:

  29. $ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );

  30. // Add page after the URL to query the information, but the value to be assigned is:

  31. If ($ url_query) $ url. = "& page"; else $ url. = "page ";
  32. } Else {
  33. $ Url. = "? Page ";
  34. }

  35. // Page number calculation:

  36. $ Lastpg = ceil ($ total/$ displaypg); // The last page, also the total number of pages
  37. $ Page = min ($ lastpg, $ page );
  38. $ Prepg = $ page-1; // Previous page
  39. $ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page
  40. $ Firstcount = ($ page-1) * $ displaypg;

  41. // Start the paging navigation bar code:

  42. $ Pagenav = "display". ($ Total? ($ Firstcount + 1): 0 )."-". Min ($ firstcount + $ displaypg, $ total )."Total $ total records
    ";

  43. // If there is only one page, the function will jump out:

  44. If ($ lastpg <= 1) return false;

  45. $ Pagenav. = "homepage ";

  46. If ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page ";
  47. If ($ nextpg) $ pagenav. = ""; else $ pagenav. = "";
  48. $ Pagenav. = "Last page ";

  49. // Pull-down Jump List, listing all page numbers cyclically:

  50. $ Pagenav. ="\ N ";For ($ I = 1; $ I <= $ lastpg; $ I ++ ){If ($ I = $ page) $ pagenav. ="$ I\ N ";Else $ pagenav. ="$ I\ N ";}$ Pagenav. ="Page, total $ lastpg page ";
  51. }
  52. }
  53. ?>

2. mysql paging call demonstration:

  1. $ DatabaseServer = "localhost ";

  2. $ UserName = "root ";
  3. $ PassWord = "";
  4. $ DatabaseName = "dede ";

  5. $ Conn = mysql_connect ($ DatabaseServer, $ UserName, $ PassWord) or die ("database connection error" + mysql_error ());

  6. Mysql_select_db ($ DatabaseName, $ conn );
  7. Mysql_query ("set names gbk ");
  8. Include ("Pages. php"); // contains the "pages. php" file.

  9. // Obtain the total number of information

  10. $ Result = mysql_query ("select * from dede_area ");
  11. $ Total = mysql_num_rows ($ result );

  12. // Call pages () to display 10 messages per page (this parameter can be omitted when the default value is 20). use the URL on this page (which is omitted by default ).

  13. Pages ($ total, 10 );

  14. // Use global variables

  15. $ Result = mysql_query ("select * from dede_area limit $ firstcount, $ displaypg ");
  16. While ($ row = mysql_fetch_array ($ result )){
  17. // (List content omitted)
  18. Echo ($ row ['eid']);
  19. Echo ($ row ['name']);
  20. Echo ("

    ");

  21. }

  22. // Output the paging navigation bar

  23. Echo $ pagenav;
  24. // (Subsequent procedures)
  25. ?>

Paging effect of php + mysql,

Recommended reading:
  • Php and ajax without refreshing paging code
  • Php article paging implementation code
  • Php limit paging code
  • Php paging class with multiple paging methods
  • Php paging code for the previous and next pages
  • Top 10 pages and top 10 pages of php paging code
  • Example of simple php paging code
  • A good php paging code
  • One paging function: next page on the previous page
  • A handy php paging class
  • Php long article paging code
  • A Practical php paging class
  • Quick php paging

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.