Table of Contents
PHP pagination code for previous page and next page
Home Backend Development PHP Tutorial php+mysql paging query code and demonstration example

php+mysql paging query code and demonstration example

Jul 25, 2016 am 08:52 AM

  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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles