PHP paging class usage _ PHP Tutorial

Source: Internet
Author: User
The usage of the PHP paging class. In this article, we will introduce in detail the code example of the PHP paging class :? To avoid errors caused by repeated file inclusion, php adds the condition for determining whether a function exists: in this article, we will give you a detailed introduction

PHP paging code example:

  1. <? Php
  2. // To avoid errors caused by repeated file inclusion,
    Added conditions for determining whether a function exists:
  3. If (! Function_exists (pageft )){
  4. // Define the pageft () function. The three parameters are described as follows:
  5. // $ Totle: total information;
  6. // $ Displaypg: number of information displayed on each page. the default value is 20;
  7. // $ Url: link in the paging navigation, except for adding different
    The parts outside the "page" of the query information are the same as the URL.
  8. // The default value should be the URL of the current page (that is, $ _ SERVER
    ["REQUEST_URI"]), but set the right side of the default value.
    It can only be a constant. Therefore, this default value is set to an empty string and is set to a URL on this page within the function.
  9. Function pageft ($ totle, $ displaypg = 20, $ url = ''){
  10. // Define several global variables:
  11. // $ Page: current page number;
  12. // $ Firstcount: (database) The start item of the query;
  13. // $ Pagenav: page navigation bar code, which is not output in the function;
  14. // $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page.
  15. Global $ page, $ firstcount, $ pagenav, $ _ SERVER;
  16. // To enable external function access to "$ displaypg"
    And set it as a global variable. Note that a variable is redefined.
    If it is a global variable, the original value is overwritten, so the value is assigned again here.
  17. $ GLOBALS ["displaypg"] = $ displaypg;
  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_urlparse_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,
    We need to remove it to add new page number information.
  26. // The regular expression is used here.
  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 ($ totle/$ 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". ($ Totle? ($ Firstcount + 1): 0 )."
    -". Min ($ firstcount + $ displaypg, $ totle )."
    A total of $ totle records
    ";
  43. // If there is only one page, the function will jump out:
  44. If ($ lastpg<= 1) return false;
  45. $ Pagenav. = "<a href = '$ url = 1'> homepage </a> ";
  46. If ($ prepg) $ pagenav. = "<a href ='
    $ Url = $ prepg '> Previous Page </a> "; else $ pagenav. =" previous page ";
  47. If ($ nextpg) $ pagenav. ="
    <A href = '$ url = $ nextpg'> Next page </a> "; else $ pagenav. =" ";
  48. $ Pagenav. = "<a href = '$ url = $ lastpg'> last page </a> ";
  49. // Pull-down Jump List, listing all page numbers cyclically:
  50. $ Pagenav. = "to the <select name = 'topage'
    Size = '1' onchange = 'window. location = "$ url =" + this. value'>N ";
  51. For ($ I = 1; $ I<= $ Lastpg; $ I ++ ){
  52. If ($ I = $ page) $ pagenav. = "<
    Option value = '$ I' selected> $ I </option> n ";
  53. Else $ pagenav. ="
    $ I </option> n ";
  54. }
  55. $ Pagenav. = "</select> Page, total $ lastpg page ";
  56. }
  57. }
  58. ?>

The above is the detailed usage of the PHP paging class, and I hope it will help you.


Example code of the pagination class in PHP :? Php // to avoid repeated file inclusion errors, the following conditions are added to determine whether a function exists :...

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.