Home  >  Article  >  Backend Development  >  php+mysql paging query code and demonstration example

php+mysql paging query code and demonstration example

WBOY
WBOYOriginal
2016-07-25 08:52:17951browse
  1. //In order to avoid errors caused by repeated inclusion of files, a condition is added to determine whether the function exists:

  2. if(!function_exists(pages)){
  3. //Define function pages (), the meanings of the three parameters are:
  4. //$total: the total number of information;
  5. //$displaypg: the number of information displayed on each page, the default setting here is 20;
  6. //$url: the link in the paging navigation, Except for adding different query information "page", the other parts are the same as this URL.
  7. //The default value should be set to the URL of this page (i.e. $_SERVER["REQUEST_URI"]), but the right side of the default value can only be a constant, so the default value is set to an empty string, and then set to inside the function. URL of this page.
  8. function pages($total,$displaypg=20,$url=''){

  9. //Define several global variables:

  10. //$page: current page number;
  11. //$ firstcount: the starting item of (database) query;
  12. //$pagenav: page navigation bar code, it is not output inside the function;
  13. //$_SERVER: read the URL of this page“$_SERVER["REQUEST_URI"]&rdquo ; Necessary.
  14. global $page,$firstcount,$pagenav,$_SERVER;

  15. //In order to make the "$displaypg" here accessible from outside the function, set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so it is reassigned here.

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

  19. //if $ If the url uses the default value, which is a null value, then the value is assigned to the URL of this page:

  20. if(!$url){ $url=$_SERVER["REQUEST_URI"];}

  21. //URL analysis:

  22. $parse_url=parse_url($url);
  23. $url_query=$parse_url["query"]; //Get the query string of the URL separately
  24. if($url_query){
  25. //Because the URL may contain page number information, It needs to be removed so that new page number information can be added.
  26. //Regular expression
  27. $url_query=ereg_replace("(^|&)page=$page","",$url_query);

  28. //Query of the processed URL String replaces the query string of the original URL:

  29. $url=str_replace($parse_url["query"],$url_query,$url);

  30. //Add page query after the URL Information, but to be assigned:

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

  35. //Page number calculation:

  36. $lastpg=ceil($total/$displaypg); //The last page is also the total page number
  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;< /p>
  41. //Start paging navigation bar code:

  42. $pagenav="Display the ".($total?($firstcount+1):0)."- ".min($firstcount+$displaypg,$total)." records, total $total records
    ";

  43. //If If there is only one page, the function will jump out:

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

  45. $pagenav.=" Homepage< /a> ";

  46. if($prepg) $pagenav.=" Previous page "; else $pagenav.=" Previous page";
  47. if($nextpg) $pagenav.=" Next page "; else $pagenav.=" Next page";
  48. $pagenav.=" < ;a href='$url=$lastpg'>Last page ";

  49. //Pull down the jump list and loop through all page numbers:

  50. $pagenav.= "Go to page of $lastpg";
  51. }
  52. }
  53. ?>< /p>
Copy code

2. Mysql paging call demonstration:

  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. //Get the total number of information

  10. $result=mysql_query("select * from dede_area ");
  11. $total=mysql_num_rows($result);

  12. //Call pages() to display 10 pieces of information per page (when using the default 20, this parameter can be omitted), use URL of this page (default, so omitted).

  13. pages($total,10);

  14. //Use global variables

  15. $result=mysql_query("select * from dede_area limit $firstcount,$displaypg ");
  16. while($row= mysql_fetch_array($result)){
  17. //(list content omitted)
  18. echo($row['eid']);
  19. echo ($row['name']);
  20. echo ("

    ");

  21. }

    +mysql paging effect, as shown in the figure:
  22. Recommended reading:
php and ajax no refresh paging code
php article paging implementation code
php limit page turning (pagination) code

php+mysql 分页效果PHP paging class with multiple paging methods

PHP pagination code for previous page and next page

    PHP paging code for the first ten pages and the next ten pages
  • Example of simple php pagination code
  • A good php pagination code
  • A paging function: Previous page Next page
  • A useful php paging class
  • php long article pagination code
  • A practical php paging class
  • Fast php paging class
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn