// To avoid errors caused by repeated file inclusion, the following conditions are added to determine whether a function exists:
- If (! Function_exists (pages )){
- // Defines the function pages (). The meaning of the three parameters is:
- // $ Total: 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 pages ($ total, $ 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;
- $ Page = $ _ GET ['Page'];
- 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, remove it to add new page number information.
- // Regular expression
- $ 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 ($ total/$ 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". ($ Total? ($ Firstcount + 1): 0 )."-". Min ($ firstcount + $ displaypg, $ total )."Total $ total 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. ="$ I\ N ";Else $ pagenav. ="$ I\ N ";}$ Pagenav. ="Page, total $ lastpg page ";
- }
- }
- ?>
2. mysql paging call demonstration:
$ DatabaseServer = "localhost ";
- $ UserName = "root ";
- $ PassWord = "";
- $ DatabaseName = "dede ";
$ Conn = mysql_connect ($ DatabaseServer, $ UserName, $ PassWord) or die ("database connection error" + mysql_error ());
- Mysql_select_db ($ DatabaseName, $ conn );
- Mysql_query ("set names gbk ");
- Include ("Pages. php"); // contains the "pages. php" file.
// Obtain the total number of information
- $ Result = mysql_query ("select * from dede_area ");
- $ Total = mysql_num_rows ($ result );
// 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 ).
- Pages ($ total, 10 );
// Use global variables
- $ Result = mysql_query ("select * from dede_area limit $ firstcount, $ displaypg ");
- While ($ row = mysql_fetch_array ($ result )){
- // (List content omitted)
- Echo ($ row ['eid']);
- Echo ($ row ['name']);
- Echo ("
");
- }
// Output the paging navigation bar
- Echo $ pagenav;
- // (Subsequent procedures)
- ?>
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
|