Php paging code. In order to avoid repeated file inclusion errors, the paging code of the php Tutorial adds the condition to determine whether the function exists: $ page $ _ get [page]; if (! Function_exists (pageft) {define function
Php Tutorial paging code
// To avoid errors caused by repeated file inclusion, the following conditions are added to determine whether a function exists:
$ Page = $ _ get [page];
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: the link in the paging navigation. except for adding different query information "page", the link is the same as the url.
// 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.
Function pageft ($ totle, $ displaypg = 20, $ url = ''){
// Define several global variables:
// $ Page: current page number;
// $ Firstcount: (database tutorial) 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", 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.
$ 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_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. See "regular expressions in php"
$ Url_query = preg_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. = "homepage ";
If ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page ";
If ($ nextpg) $ pagenav. = ""; else $ pagenav. = "";
$ Pagenav. = "Last page ";
// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. ="N ";For ($ I = 1; $ I <= $ lastpg; $ I ++ ){If ($ I = $ page) $ pagenav. ="$ IN ";Else $ pagenav. ="$ IN ";}$ Pagenav. ="Page, total $ lastpg page ";
}
}
Else // to avoid errors caused by repeated file inclusion, the condition for determining whether a function exists is added: $ page =$ _ get [page]; if (! Function_exists (pageft) {// defines the function...