PHP custom paging function. PHP custom paging function every time you write a list page, you need to write a paging program. Recently, Xiaoyang is determined to define a PHP custom paging function once and for all.
PHP custom paging function
Every time you write a list page, you need to write a paging program. Recently, Xiaoyang was determined to create a paging function and save it in the "pageft. php" file. To use paging, you can directly include the "pageft. php" file and call this function. When writing this function, Xiaoyang tries to pay attention to the portability and ease-of-use of the program. below is the function compiled by Xiaoyang:
// To avoid errors caused by repeated file inclusion, the following conditions are added to determine 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: 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) 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", 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.
// Here the regular expression is used, please refer to "regular expression in PHP" (http://www.pconline.com.cn/pcedu/empolder/wz/php/10111/15058.html)
$ 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 = "show th". ($ totle? ($ Firstcount + 1): 0). "-". min ($ firstcount + $ displaypg, $ totle). "records, total $ 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. = "to \ n"; for ($ I = 1; $ I <= $ lastpg; $ I ++) {if ($ I = $ page) $ pagenav. = "$ I \ n"; else $ pagenav. = "$ I \ n";} $ pagenav. = "page, total $ lastpg page ";
}
}
?>
Well, the paging function has been written and saved as "pageft. php". it is included in the paging function and the pageft () function is called. However, it does not output anything, but generates several global variables: $ firstcount, $ displaypg, and $ pagenav. The following is an example of its usage:
// (Previous program omitted)
Include ("pageft. php"); // contains the "pageft. php" file.
// Obtain the total number of information
$ Result = mysql_query ("select * from mytable ");
$ Total = mysql_num_rows ($ result );
// Call pageft () 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 ).
Pageft ($ total, 10 );
// The global variables generated now come in handy:
$ Result = mysql_query ("select * from mytable limit $ firstcount, $ displaypg ");
While ($ row = mysql_fetch_array ($ result )){
// (List content omitted)
}
// Output the pagination navigation bar code:
Echo $ pagenav;
// (Subsequent procedures)
?>
Http://www.bkjia.com/PHPjc/845138.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/845138.htmlTechArticlePHP custom paging function PHP custom paging function every time you write a list of page nature, almost all have to write a piece of paging program. Recently, Xiaoyang is determined to define a new one once and for all...