Table of Contents
Silently talk about PHP&MYSQL paging principles and implementation
Home Backend Development PHP Tutorial Silently talk about PHP@MYSQL paging principle and implementation_PHP tutorial

Silently talk about PHP@MYSQL paging principle and implementation_PHP tutorial

Jul 13, 2016 am 10:59 AM
Pagination principle accomplish

Silently talk about PHP&MYSQL paging principles and implementation

Before reading this article, please make sure you have mastered some knowledge of PHP and the basics of MYSQL query operations.

As a web program, it often has to deal with countless data, such as member data and article data. If there are only a few dozen members, it is easy to handle. It can be displayed on one page, but if your website If there are thousands or even hundreds of thousands of members, if they are all opened on one page, it will be a torture for both the browser and the viewer.

I believe that every novice learning PHP will have a headache about pagination, but with this post from Mo Mo, you will definitely pat your head and say, hey, it turns out that pagination is so simple? Indeed, please take a deep breath of fresh air now and listen carefully as Silence explains it to you bit by bit.

Suppose we want to process 1,000 pieces of data and display 10 pieces on each page. In this case, it will be displayed in 100 pages. Let's first take a look at how to extract 10 pieces of information in mysql.

Select * from table limit 0,10

The above is a very simple mysql query statement. Its function is to extract 10 pieces of data from a table named table and obtain the values ​​of all fields.

The key point is in this section "limit 0,10". The 0 in it is 0 as the starting point, and the following 10 is to display 10 pieces of data. Then we need to use 10 as the starting point to display to the How to write 20 pieces of data?

Maybe a lot of people say "limit 10,20" outright! Oh, this is wrong. The correct way to write it is "limit 10,10". The parameter after it is not the end point but the number to be extracted. Remember.

Now that you know how to extract 10 pieces of data, extracting 1,000 pieces means doing this kind of query 100 times, which means you have to do the following query:

Limit 0,10 //First page
Limit 10,10 //Second page
Limit 20,10 //Page 3
Limit 30,10 //Page 4
……
Do you see any pattern? Yes, the first parameter increases by 10 every time the page is turned, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter according to the number of pages, we can display the data in pages. How about it? Is the principle very simple?

But how to change the value of the first parameter according to the number of pages? First, we need to have a page number value, which can be obtained using the GET method of the URL.
For example index.php?page=18
I believe most of you are familiar with this thing. This kind of URL address can be found everywhere. The function of the page parameter is to pass in the number of pages to be displayed.

Let’s take a look at how it is implemented through a piece of code:
Copy PHP content to clipboard PHP code:

/*

Author: Silently
Date :2006-12-03

*/

$page=isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, Then the page number is 1.
$num=10;                                                                                                                                                                                        

$db=mysql_connect("host","name","pass"); //Create database connection

$select=mysql_select_db("db",$db); //Select the database to operate

/*

First, we need to get how much data there is in the database to determine how many pages it needs to be divided into. The specific formula is
Divide the total number of data by the number of items displayed on each page, and the remainder is rounded to one.
In other words, 10/3=3.3333=4 If there is a remainder, we must round it up by one.
*/

$total=mysql_num_rows(mysql_query("select id from table")); //Query the total number of data, id is an automatically assigned field in the database

$ Pagenum = CEIL ($ Total/$ Num); // Get the total page

//If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
$totalrecord=mysql_num_rows($result);
$pagesize=10;
$page=isset($_GET['page'])?$_GET['page']:1;
$pagecount=($totalrecord % $pagesize==0)?$totalrecord / $pagesize:(int) ($totalrecord / $pagesize)+1;
$page=($page>$pagecount || $page<1 )?1:$page;
$start=$pagesize*($page-1);
$res=mysql_query("select * from tablename order by id desc limit $start,$pagesize")

//Get the data that needs to be displayed for the corresponding page number, name is a field in the data
While($rsmysql_fetch_array($res)){
Echo $rs['name']."
";
}                                                                                                                                                                                                      ​ 
I’ll use for below, so I won’t go into details. There are many other methods, and they are indispensable.

/*Display paging information. If it is the current page, bold numbers will be displayed. The remaining page numbers are hyperlinks. If it is the third page, it will be displayed as follows

1 2 3 4 5 6
*/
?>

If you read the above code carefully and replace the database connection and query tables with yours, you can see its execution effect.

http://www.bkjia.com/PHPjc/631898.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631898.htmlTechArticleSilently talk about PHPMYSQL paging principles and implementation. Before reading this article, please make sure you have mastered some knowledge of PHP and Basics of MYSQL query operations. As a web program, you often have to interact with...
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 to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

An in-depth analysis of the functions and working principles of the Linux chage command An in-depth analysis of the functions and working principles of the Linux chage command Feb 24, 2024 pm 03:48 PM

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

See all articles