Pagination in php

Source: Internet
Author: User
This article mainly introduces how to implement paging display in php. if you are interested, you can refer to the so-called paging display, which means that the result set in the database is displayed in a segment, 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.
For other parameters, such:($ NextPageID), total number of pages ($ numPages)And so on. they can all be obtained based on previous knowledge.

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.

The 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.
Simple code implementation
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.

// Create 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;} // page size $ 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, only one page if ($ amount % $ page_size) {// divide the total data volume by the remainder of each page. $ page_count = (int) ($ amount/$ page_size) + 1; // if there is remainder, the number of pages equals to the total data volume divided by the result of each page number rounded up plus 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;} // page flip link $ page_string = ''; if ($ page = 1) {$ page_string. = 'first page | last page ';} else {$ page_string. = '. ($ Page + 1 ). '> Next Page |. $ page_count. '> last page';} // retrieves data and returns the result 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 displayed result is out of scope, with foreach, you can simply use the two-dimensional array to display the result?>

The above is the method for implementing paging display in php. I hope it will be helpful for your learning.

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.