Home Backend Development PHP Tutorial A PHP paging function

A PHP paging function

Jul 25, 2016 am 08:44 AM

PHP paging code call: $start=show_page($query,$page,$link,$offset); where: $start is the starting record, $query is the full record retrieval SQL statement, $page is the current page number, $link is the passed The page parameter $offset is the number of records displayed on each page

  1. //======function.php======
  2. //======Paging function=== =======
  3. function show_page($query,$page,$link,$offset)
  4. {
  5. $db = new mysql();
  6. $result = $db->query($query);
  7. $Page_size = $offset; //Get the maximum number of orders displayed on each page
  8. $count = $db->affected_rows($result); //Total number of orders
  9. $page_count = ceil($count/$Page_size); / /Calculate the total number of pages
  10. $init=1;
  11. $page_len=7;
  12. $max_p=$page_count;
  13. $pages=$page_count;
  14. //Judge the current page number
  15. $page=(empty($page )||$page<0)?1:$page;
  16. $start=$Page_size*($page-1);
  17. //Paging function code
  18. $page_len = ($page_len%2)?$page_len:$ pagelen+1; //The number of page numbers
  19. $pageoffset = ($page_len-1)/2; //The left and right offset of the number of page numbers
  20. $key="$count in total";
  21. $key.="$ page/$pages "; //What page, how many pages in total
  22. if($page!=1){
  23. $key.="First page "; //First page
  24. $key.="Previous page"; //Previous page
  25. }
  26. else
  27. {
  28. $key.="First page";//First Page
  29. $key.="Previous page"; //Previous page
  30. }
  31. if($pages>$page_len)
  32. {
  33. //If the current page is less than or equal to the left offset
  34. if($page<=$pageoffset ){
  35. $init=1;
  36. $max_p = $page_len;
  37. }
  38. else //If the current page is greater than the left offset
  39. {
  40. //If the right offset of the current page number exceeds the maximum number of pages
  41. if($page+$ pageoffset>=$pages+1){
  42. $init = $pages-$page_len+1;
  43. }
  44. else
  45. {
  46. //Calculation when both left and right offsets exist
  47. $init = $page-$pageoffset;
  48. $ max_p = $page+$pageoffset;
  49. }
  50. }
  51. }
  52. for($i=$init;$i<=$max_p;$i++)
  53. {
  54. if($i==$page){$key.=' ['.$i.']';}
  55. else {$key.=" ";}
  56. }
  57. if($page!=$pages)
  58. {
  59. $key.=" Next page ";//Next page
  60. $key.="Last page"; //Last page
  61. }
  62. else
  63. {
  64. $key.= "Next page"; //Next page
  65. $key.="Last page"; //Last page
  66. }
  67. echo "$key

    ";
  68. return $start;
  69. }
  70. ?>
Copy code

Call example

[code]
Pagination, PHP


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)

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

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

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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

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.

See all articles