Home Backend Development PHP Tutorial Analysis of PHP paging principle Attachment: Simple example_PHP tutorial

Analysis of PHP paging principle Attachment: Simple example_PHP tutorial

Jul 21, 2016 pm 02:55 PM
php analyze Pagination Preface principle and Example data method yes show Browse of Simple No

1. Introduction

Paging display is a very common method of browsing and displaying large amounts of data, and is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail, and strive to let readers Friends who have finished this article will have some understanding of the principles and implementation methods of paging display. This article is suitable for beginners to read, and all sample codes are written in PHP.

2. Principle

The so-called paging display means that the result set in the database is artificially divided into sections for display. Two initial parameters are required here:

How many records per page ($PageSize)?
What page is the current page ($CurrentPageID)?

Now as long as you give me another result set, I can display a specific result.
As for other parameters, such as: previous page ($PreviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can all be obtained based on the previous things.
Take the mysql database as an example. If you want to intercept a certain piece of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.

The first 10 records: select * from table limit 0,10
The 11th to 20th records: select * from table limit 10,10
The 21st to 30th records: select * from table limit 20,10
......

This set of sql statements is actually the sql statement that fetches data from each page of the table when $PageSize=10. We can summarize such a template:

select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize

Take this template and substitute the corresponding value with the above set of sql statements to see if it is the same. thing. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it. Below I will explain it with specific code.

3. Simple code
Please read the following code in detail, debug and run it yourself. It is best to modify it once and add your own functions, such as search, etc.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] //Establish a database connection
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
//Get the current page number
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
// Number of pages per page
$PageSize = 10;
// Get the total data volume
$sql = "select count(* ) as amount from table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
// Calculate how many pages there are in total
if( $amount ){
if( $amount < $page_size ){ $page_count = 1; } //If the total data amount is less than $PageSize, then there is only one page
if( $amount % $page_size ){ //Get the remainder of the total data amount divided by the number of pages
$page_count = (int)($amount / $page_size) + 1; //If there is a remainder, the page The number is equal to the total data amount divided by the number of pages. The result is rounded and plus one
}else{
$page_count = $amount / $page_size; //If there is no remainder, the number of pages is equal to the total data amount divided by Results of the number of pages per page
}
}
else{
$page_count = 0;
}

// Page turning link
$page_string = '';
if( $page == 1 ){
$page_string .= 'First page|Previous page|';
}
else{
$page_string .= '|Previous page}
if( ($page == $page_count) || ($page_count == 0) ){
$page_string .= 'next page|last page';
}
else{
$page_string .= '
Next page|Last page';
}
// Get data and return the result in two-dimensional array format
if( $ amount ){
$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);

while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// There is no code to display the results, so that is beyond the scope of discussion. As long as you use foreach, you can easily use the obtained two-dimensional array to display the results
?>

  • Total 3 pages:
  • Previous page
  • 1
  • 2
  • 3
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364454.htmlTechArticle1. Preface Pagination display is a very common method of browsing and displaying large amounts of data, which is the most common method in web programming. One of the commonly handled events. For web programming veterans, writing this code...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles