To avoid errors caused by repeating the inclusion of a file, add a condition that determines whether the function exists:
- if (!function_exists (pages)) {
- Define the function pages (), the meaning of the three parameters is:
- $total: Total information;
- $DISPLAYPG: The number of messages displayed per page, which is set to default is 20;
- $url: The link in the pager is the same as this one except for adding different query Information "page".
- The default value should be set to the page URL (that is, $_server["Request_uri"]), but the default value can only be constant to the right, so the default value is set to an empty string, which is then set to the page URL inside the function.
- function pages ($total, $displaypg =20, $url = ") {
Define several global variables:
- $page: current page number;
- $firstcount: The starting item of the (database) query;
- $pagenav: the page navigation bar code, inside the function does not output it;
- $_server: Read this page url "$_server[" Request_uri "]" must.
- Global $page, $firstcount, $pagenav, $_server;
For the outside of the function to have access to the "$DISPLAYPG" here, it is also set as a global variable. Note that when a variable is redefined as a global variable, the original value is overwritten, so it is re-assigned.
- $GLOBALS ["Displaypg"]= $displaypg;
- $page =$_get[' page ';
- if (! $page) $page = 1;
If you use the default, or null value, for the value of the page, the URL is assigned:
- if (! $url) {$url =$_server["Request_uri"];}
URL Analysis:
- $parse _url=parse_url ($url);
- $url _query= $parse _url["Query"]; Query string to take out the URL separately
- if ($url _query) {
- Because the URL may contain page number information, to remove it, in order to add new page number information.
- Regular expressions
- $url _query=ereg_replace ("(^|&) page= $page", "", $url _query);
Replace the query string for the processed URL with the query string for the original URL:
- $url =str_replace ($parse _url["Query"], $url _query, $url);
Add page query information after URL, but to assign value:
- if ($url _query) $url. = "&page"; else $url. = "page";
- }else {
- $url. = "? page";
- }
Page Count:
- $LASTPG =ceil ($total/$DISPLAYPG); 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 Page navigation bar code:
- $pagenav = "Show Section ". ( $total? ($firstcount + 1): 0). " -". Min" ($firstcount + $DISPLAYPG, $total). " Records, Total $total records
";
If only one page jumps out of the function:
- if ($lastpg <=1) return false;
$pagenav. = "Home";
- if ($PREPG) $pagenav. = "front page"; else $pagenav. = "front page";
- if ($NEXTPG) $pagenav. = "Back Page"; else $pagenav. = "Back Page";
- $pagenav. = "Last";
Drop-down Jump List, looping through all page numbers:
- $pagenav. = "To\ n ", for ($i =1; $i <= $lastpg; $i + +) {if ($i = = $page) $pagenav. ="$i \ n "; else $pagenav. ="$i \ n ";} $pagenav. = "Page, total $lastpg page ";
- }
- }
- ?>
Copy Code2. mysql Paging call Demo:
$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"); Include "pages.php" file
Total number of information obtained
- $result =mysql_query ("SELECT * from Dede_area");
- $total =mysql_num_rows ($result);
Call pages (), displaying 10 messages per page (this parameter can be omitted using the default 20 o'clock), using the page URL (default, so omitted).
- Pages ($total, 10);
Global variables Use
- $result =mysql_query ("select * from Dede_area limit $firstcount, $displaypg");
- while ($row =mysql_fetch_array ($result)) {
- (List content slightly)
- Echo ($row [' Eid ']);
- Echo ($row [' name ']);
- Echo ("
");
- }
Output Paging navigation bar
- Echo $pagenav;
- (later procedure slightly)
- ?>
Copy CodePhp+mysql pagination effect, Recommended reading:
- PHP and Ajax No Refresh paging code
- PHP article pagination Implementation code
- PHP Limit Paging (paging) code
- PHP Paging class with a variety of paging methods
- Previous page and next page of PHP page code
- PHP pagination code for the first 10 pages and the next 10 pages
- Example of a simple PHP page-out code
- A nice code for the PHP paging class
- A paging function: previous page next page
- A handy PHP paging class
- PHP Long article pagination code
- A useful PHP page-out class
- Fast PHP page-out class
|