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:
- <? Php
- // To avoid errors caused by repeated file inclusion,
Added conditions for determining whether a function exists:
- If (! Function_exists (pageft )){
- // Define the pageft () function. The three parameters are described as follows:
- // $ Totle: total information;
- // $ Displaypg: number of information displayed on each page. the default value is 20;
- // $ 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.
- // 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.
- Function pageft ($ totle, $ displaypg = 20, $ url = ''){
- // Define several global variables:
- // $ Page: current page number;
- // $ Firstcount: (database) The start item of the query;
- // $ Pagenav: page navigation bar code, which is not output in the function;
- // $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page.
- Global $ page, $ firstcount, $ pagenav, $ _ SERVER;
- // 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.
- $ GLOBALS ["displaypg"] = $ displaypg;
- If (! $ Page) $ page = 1;
- // If $ url uses the default value, that is, null value, the value is assigned to the URL of the current page:
- If (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"];}
- // URL analysis:
- $ Parse_urlparse_url = parse_url ($ url );
- $ Url_query = $ parse_url ["query"]; // Retrieve the query string of the URL separately.
- If ($ url_query ){
- // Because the URL may contain page number information,
We need to remove it to add new page number information.
- // The regular expression is used here.
- $ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );
- // Replace the query string of the processed URL with the query string of the original URL:
- $ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );
- // Add page after the URL to query the information, but the value to be assigned is:
- If ($ url_query) $ url. = "& page"; else $ url. = "page ";
- } Else {
- $ Url. = "? Page ";
- }
- // Page number calculation:
- $ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
- $ Page = min ($ lastpg, $ page );
- $ Prepg = $ page-1; // Previous page
- $ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page
- $ Firstcount = ($ page-1) * $ displaypg;
- // Start the paging navigation bar code:
- $ Pagenav = "display". ($ Totle? ($ Firstcount + 1): 0 )."
-". Min ($ firstcount + $ displaypg, $ totle )."
A total of $ totle records
";
- // If there is only one page, the function will jump out:
- If ($ lastpg<= 1) return false;
- $ Pagenav. = "<a href = '$ url = 1'> homepage </a> ";
- If ($ prepg) $ pagenav. = "<a href ='
$ Url = $ prepg '> Previous Page </a> "; else $ pagenav. =" previous page ";
- If ($ nextpg) $ pagenav. ="
<A href = '$ url = $ nextpg'> Next page </a> "; else $ pagenav. =" ";
- $ Pagenav. = "<a href = '$ url = $ lastpg'> last page </a> ";
- // Pull-down Jump List, listing all page numbers cyclically:
- $ Pagenav. = "to the <select name = 'topage'
Size = '1' onchange = 'window. location = "$ url =" + this. value'>N ";
- For ($ I = 1; $ I<= $ Lastpg; $ I ++ ){
- If ($ I = $ page) $ pagenav. = "<
Option value = '$ I' selected> $ I </option> n ";
- Else $ pagenav. ="
$ I </option> n ";
- }
- $ Pagenav. = "</select> Page, total $ lastpg page ";
- }
- }
- ?>
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 :...