Php+mysql Paging Query Code and demo example

Source: Internet
Author: User
  1. To avoid errors caused by repeating the inclusion of a file, add a condition that determines whether the function exists:

  2. if (!function_exists (pages)) {
  3. Define the function pages (), the meaning of the three parameters is:
  4. $total: Total information;
  5. $DISPLAYPG: The number of messages displayed per page, which is set to default is 20;
  6. $url: The link in the pager is the same as this one except for adding different query Information "page".
  7. 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.
  8. function pages ($total, $displaypg =20, $url = ") {

  9. Define several global variables:

  10. $page: current page number;
  11. $firstcount: The starting item of the (database) query;
  12. $pagenav: the page navigation bar code, inside the function does not output it;
  13. $_server: Read this page url "$_server[" Request_uri "]" must.
  14. Global $page, $firstcount, $pagenav, $_server;

  15. 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.

  16. $GLOBALS ["Displaypg"]= $displaypg;
  17. $page =$_get[' page ';
  18. if (! $page) $page = 1;

  19. If you use the default, or null value, for the value of the page, the URL is assigned:

  20. if (! $url) {$url =$_server["Request_uri"];}

  21. URL Analysis:

  22. $parse _url=parse_url ($url);
  23. $url _query= $parse _url["Query"]; Query string to take out the URL separately
  24. if ($url _query) {
  25. Because the URL may contain page number information, to remove it, in order to add new page number information.
  26. Regular expressions
  27. $url _query=ereg_replace ("(^|&) page= $page", "", $url _query);

  28. Replace the query string for the processed URL with the query string for the original URL:

  29. $url =str_replace ($parse _url["Query"], $url _query, $url);

  30. Add page query information after URL, but to assign value:

  31. if ($url _query) $url. = "&page"; else $url. = "page";
  32. }else {
  33. $url. = "? page";
  34. }

  35. Page Count:

  36. $LASTPG =ceil ($total/$DISPLAYPG); Last page, also the total number of pages
  37. $page =min ($LASTPG, $page);
  38. $PREPG = $page-1; Previous page
  39. $NEXTPG = ($page = = $lastpg? 0: $page + 1); Next page
  40. $firstcount = ($page-1) * $DISPLAYPG;

  41. Start Page navigation bar code:

  42. $pagenav = "Show Section ". ( $total? ($firstcount + 1): 0). " -". Min" ($firstcount + $DISPLAYPG, $total). " Records, Total $total records
    ";

  43. If only one page jumps out of the function:

  44. if ($lastpg <=1) return false;

  45. $pagenav. = "Home";

  46. if ($PREPG) $pagenav. = "front page"; else $pagenav. = "front page";
  47. if ($NEXTPG) $pagenav. = "Back Page"; else $pagenav. = "Back Page";
  48. $pagenav. = "Last";

  49. Drop-down Jump List, looping through all page numbers:

  50. $pagenav. = "To\ n ", for ($i =1; $i <= $lastpg; $i + +) {if ($i = = $page) $pagenav. ="$i \ n "; else $pagenav. ="$i \ n ";} $pagenav. = "Page, total $lastpg page ";
  51. }
  52. }
  53. ?>

Copy Code

2. mysql Paging call Demo:

  1. $DatabaseServer = "localhost";

  2. $UserName = "root";
  3. $PassWord = "";
  4. $DatabaseName = "Dede";

  5. $conn =mysql_connect ($DatabaseServer, $UserName, $PassWord) or Die ("Database connection Error" +mysql_error ());

  6. mysql_select_db ($DatabaseName, $conn);
  7. mysql_query ("Set names GBK");
  8. Include ("pages.php"); Include "pages.php" file

  9. Total number of information obtained

  10. $result =mysql_query ("SELECT * from Dede_area");
  11. $total =mysql_num_rows ($result);

  12. 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).

  13. Pages ($total, 10);

  14. Global variables Use

  15. $result =mysql_query ("select * from Dede_area limit $firstcount, $displaypg");
  16. while ($row =mysql_fetch_array ($result)) {
  17. (List content slightly)
  18. Echo ($row [' Eid ']);
  19. Echo ($row [' name ']);
  20. Echo ("

    ");

  21. }

  22. Output Paging navigation bar

  23. Echo $pagenav;
  24. (later procedure slightly)
  25. ?>

Copy Code

Php+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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.