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 what if your website has several members? 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. Moreover, if the data is hundreds of millions, querying it once from the database will put a lot of pressure on the server. This is not the correct approach.
I believe that every novice learning PHP will have a headache about pagination, but with Momo’s post, 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 usage of limit 0,10 is: limit starting point, the amount to be extracted
The key part is in this paragraph "limit 0,10", where 0 is the starting point, and the following 10 is Display 10 pieces of data, then we need to use 10 as the starting point, and how to write the 20th piece of data?
Maybe many people will 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 we know how to extract 10 pieces of data, extracting 1,000 pieces means doing this kind of query 100 times, which means doing the following query:
Limit 0,10 //First page
Limit 10,10 // Second page
Limit 20,10 //Third page
Limit 30,10 //Fourth page
...
Can 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:
[php]
/*
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 number of pages That's 1.
$num=10; //Display 10 pieces of data per page
$db=mysql_connect("host","name","pass"); //Create a 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 for the total number of pages is
The total number of data divided by the display per page The number of bars is more than one.
That is to say, 10/3=3.3333=4. If there is a remainder, we must round it up by one.
*/
$total=mysql_num_rows(mysql_query("select * from table")); //The total number of query data total
$pagenum=ceil($total/$num); //Get the total number of pages pagenum
//If the page number parameter apge passed in is greater than the total page number pagenum, an error message will be displayed
If($page>$pagenum || $page == 0){
Echo "Error : Can Not Found The page .";
Exit;
}
$offset=($page-1)*$num; //Get the value offset of the first parameter of limit, if the first page is (1-1)*10=0, the first page The second page is (2-1)*10=10. (Number of pages passed in - 1) * The data of each page gets the value of the first parameter of limit
$info=mysql_query("select * from table limit $offset,$num "); //Get the corresponding page number Data to be displayed
While($it=mysql_fetch_array($info)){
Echo $it['name']."
";
} //Display data
For($i=1 ;$i<=$pagenum;$i++){
$show=($i!=$page)?"$i< ;/a>":"$i";
Echo $show." ";
}
/*Display paging information. If it is the current page, bold numbers will be displayed, and the rest will be displayed. The page number is a hyperlink. If it is the third page, it will be displayed as follows
1 2 3 4 5 6
*/
?>
[/php]
If you read the above code carefully, connect the database with Replace the queried table with yours, and then you can see its execution effect.
Isn’t it very simple? As long as you use your brain, you can make it display more personalized. Let me give you a little question, how to implement paging in the format of "Home Page, Previous Page, Next Page, Last Page"?
OK, finish filling the water posts and call it a day. ^_^ Silently talk about PHP & MYSQL paging principles and implementation
======================================================
Summary:
Prototype: Select * from table limit 0,10
Program: select * from table limit $offset,$num ($offset value is: the number of pages passed in -1 $num is the data displayed on each page, more is a fixed constant value)
Total number of pages: total data % The number of items displayed on each page, and any remainder
int totalPage=((totalCount%NUM)==0)?totalCount/NUM:totalCount/NUM+1;
Usage of limit: limit starting point, the number to be extracted
But please note: be sure to add order by, and make sure to query in ascending or descending order, otherwise you will not know which direction to start from when querying Inquire. But be sure to pay attention to the order: the correct one is select * from user order by id desc limit 0,10;
For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!