1. forward pagination is an uncommon way to browse and display large volumes of data. it is one of the most common events in web programming. For web programming veterans, writing such code is as natural as breathing, but for beginners
1. Preface
Pagination is an uncommon way to browse and display large volumes of data. it is one of the most common events in web programming. For veterans of web programming, writing such code is as natural as breathing, but for beginners, they often have no clue about this title. Therefore, I wrote this article to explain this title in detail, I hope that my friends who have read this article will understand the principle and implementation method of paging display. This article is suitable for beginners. all sample code is written in php.
2. Principles
The so-called paging display means that the result set in the database is manually divided into segments for display. here two initial parameters are required:
How many records per page (
Current page (
Now, you only need to give me another result set, so that I can display a specific result.
For other parameters, such (
Take the mysql database as an example. if you want to extract some content from the table, you can use the SQL statement: select * from table limit offset and rows. Let's take a look at the following SQL statements and try to invent the rule rate.
First 10 records: select * from table limit 0, 10
11th to 20 records: select * from table limit 10, 10
21st to 30 records: select * from table limit 20, 10
......
This group of SQL statements is the SQL statement used to retrieve data on each page of the table at = 10. we can summarize this template:
Select * from table limit (-1 )*,
Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After getting the title of the most important data, the rest is to pass the parameters, structure the appropriate SQL statement, and then use php to get data from the database and display it. The following code will be used to clarify.
3. simple code
Go to the following code and debug and run it yourself. you 'd better correct it once and add your own functions, such as search.
// Establish a database connection
= Mysql_connect ('localhost', 'MySQL _ user', 'MySQL _ password ')
Or die ('could not connect: '. mysql_error ());
// Obtain the current page number
If (isset ()){
= Intval ();
}
Else {
= 1;
}
// Per page
= 10;
// Obtain the total data volume
= 'SELECT count (*) as amount from table ';
= Mysql_query ();
= Mysql_fetch_row ();
=;
// Count the total number of pages
If (){
If (<) {= 1;} // if the total data volume is smaller
If (%) {// Retrieve the total data volume divided by the remainder of each page
= (Int) (/) 1; // if there is a remainder, the number of pages equals the total data volume divided by the result of each page and then adds one
} Else {
= // If there is no remainder, the number of pages is the result of dividing the total data volume by the number of pages
}
}
Else {
= 0;
}
// Flip link
= '';
If (= 1 ){
. = 'First page | previous page | ';
}
Else {
. = 'First page | previous page | ';