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 ($ PageSize )?
What is the current page ($ CurrentPageID )?
Now, you only need to give me another result set, so that I can display a specific result.
Other parameters, such as the previous page ($ PreviousPageID), Next page ($ NextPageID), and total page ($ numPages), can all be obtained based on the front.
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 set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ PageSize = 10. we can summarize this template:
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.
<? Php
// Establish a database connection
$ Link = mysql_connect ('localhost', 'MySQL _ user', 'MySQL _ password ')
Or die ('could not connect: '. mysql_error ());
// Obtain the current page number
If (isset ($ _ GET ['Page']) {
$ Page = intval ($ _ GET ['Page']);
}
Else {
$ Page = 1;
}
// Per page
$ PageSize = 10;
// Obtain the total data volume
$ SQL = 'SELECT count (*) as amount from table ';
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_row ($ result );
$ Amount = $ row ['amount'];
// Count the total number of pages
If ($ amount ){
If ($ amount <$ page_size) {$ page_count = 1;} // if the total data volume is less than $ PageSize, there is only one page
If ($ amount % $ page_size) {// Retrieve the total data volume divided by the remainder of each page
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service