PHP paging display production

Source: Internet
Author: User
The so-called paging display refers to dividing the result set in the database 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, and I can display a specific... SyntaxHighlighter. all ();

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, rows. Take a look at the following SQL statements and try to find 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:
Select * from table limit ($ CurrentPageID-1) * $ PageSize, $ PageSize
Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it. I will describe the code below.
3. simple code
Please read the following code in detail and debug and run it once. you 'd better modify 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 ("cocould not connect:". mysql_error ());
// Obtain the current page number
If (isset ($ _ GET ['Page']) {
$ Page = intval ($ _ GET ['Page']);
}
Else {
$ Page = 1;
}
// Number of pages 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
$ Page_count = (int) ($ amount/$ page_size) + 1; // if there is a remainder, the number of pages equals the total data volume divided by the result of each page number and then adds one
} Else {
$ Page_count = $ amount/$ page_size; // if there is no remainder, the number of pages equals the total data volume divided by the result of each page
}
}
Else {
$ Page_count = 0;
}
// Flip link
$ Page_string = '';
If ($ page = 1 ){
$ Page_string. = 'Page 1 | previous page | ';
}
Else {
$ Page_string. = 'Page 1 | previous page | ';
}
If ($ page = $ page_count) | ($ page_count = 0 )){
$ Page_string. = 'next page | last page ';
}
Else {
$ Page_string. = 'next page | last page ';
}
// Obtain data and return results in two-dimensional array format
If ($ amount ){
$ SQL = "select * from table order by id desc limit". ($ page-1) * $ page_size. ", $ page_size ";
$ Result = mysql_query ($ SQL );

While ($ row = mysql_fetch_row ($ result )){
$ Rowset [] = $ row;
}
} Else {
$ Rowset = array ();
}
// The code that does not contain the display result is not in the scope of the discussion. you only need to use foreach to easily display the result using the two-dimensional array.
?>

 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.