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

WBOY
Release: 2016-07-13 10:59:35
Original
764 people have browsed it

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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template