Explanation of php paging ideas _ PHP Tutorial

Source: Internet
Author: User
A detailed explanation of php paging methods. Pagination in php is relatively simple. we only need to understand the following variables, such as pagesize, count, and page. The total number of pages is the page number, the page displays several records. in php, pagination is relatively simple. we only need to understand the following variables, such as pagesize, count, and page, and the total number of pages, the number of records displayed on the page. we only need a formula to calculate the number of records.

You only need to get two variables to get half done:
Number of records to be displayed per page $ pageSize
Total data volume in the table $ rowCount
With the above two variables, we can get a total of several pages. $ pageCount
Then, through the for loop, for example, there are 13 pages in total, it is easy to output the number of pages through the for loop.

The code is as follows:
$ Nav = ''; // A variable used to save the number of pages
For ($ I = 1; $ I <= 13; $ I ++)
{
$ Nav. = "page". $ I ";
}

The above for loop will be output as shown in
1st pages, 2nd pages, 3rd pages, 4th pages, 5th pages, 6th pages, 7th pages, 8th pages, 9th pages, 10th pages, 11th pages, 12th pages
What if we only want to display 10 pages at a time? For example, pages 1-10 and pages 11-20
It's easy. you only need to slightly modify the for loop.

The code is as follows:
$ Step = floor ($ pageNow-1)/10) * 10 + 1;
For ($ I = $ step; $ I <= $ step + 10; $ I ++)
{
$ Nav. = "page". $ I ";
}

For example, how does the current page $ pageNow ~ Between 10, then $ step = 0
How does the current page $ pageNow go from 11 ~ Between 20, then $ step = 10
How does the current page $ pageNow go from 21 to 21 ~ Between 30, then $ step = 20
By referring to the code of the specific implementation process, we can easily find that the second condition of the for loop only needs to be added with 10 to display only 10 at a time, we split this step into the getLink () method in the fenyePage class.
Then again, how can we get the values of the $ pageSize and $ rowCount variables?
$ PageSize can be specified by programmers themselves. $ rowCount can be obtained by using a simple function that executes SQL statements.

The code is as follows:
/**
* $ SQL statement: ① obtain data ② obtain the total number of records
*/
Class fenyePage {
Public $ pageSize = 5; // The number displayed on each page --> The number specified by the programmer
Public $ rowCount; // This is obtained FROM the database (such as select count (id) from table) to save the total number of records
Public $ pageNow; // obtained through $ _ GET ['Page'] to save the current page number.
Public $ pageCount; // calculated to save the total number of pages
Public $ res_arr; // used to save the data to be displayed on the page (for example, save the data retrieved from table limit)
Public $ nav; // display the navigation bar on the page
/**
* Obtain the hyperlink of the current page
*
* @ Author Xiaofei 2012/5/30
*/
Public function getLink ()
{
$ This-> nav = '';
$ This-> pageCount = ceil ($ this-> rowCount/$ this-> pageSize ));
$ Step = floor ($ this-> pageNow-1)/10) * 10 + 1;
If ($ this-> pageNow> 10)
{
$ This-> nav. = "<"; // forward each 10 pages
}
If ($ this-> pageNow! = 1)
{
$ This-> nav. = "pageNow-1)." '> Previous Page ";
}
If ($ this-> pageNow! = 1)
{
$ This-> nav. = "homepage ";
}
For ($ start = $ step; $ start <$ step + 10 & $ start <= $ this-> pageCount; $ start ++)
{
$ This-> nav. = "". $ start ."";
}
If ($ this-> pageNow! = $ This-> pageCount)
{
$ This-> nav. = "pageCount." '> last page ";
}
If ($ this-> pageNow! = $ This-> pageCount)
{
$ This-> nav. = "pageNow + 1)." '> next page ";
}
If ($ this-> pageCount> 10 & $ this-> pageNow <$ this-> pageCount-8 ){
$ This-> nav. = ">"; // returns the result of every 10 pages.
}
$ This-> nav. = "/total". $ this-> pageCount. "page ";
}
}
?>

Since the database operation tasks in zf are completed by the model layer, I put the function for getting the $ rowCount value in the corresponding table model.
For example, I operate the order table.
When I want to display all the order information, I use the showorder () method in the order class to get the value of $ rowCount and pay it to the $ rowCount attribute in the paging class.
Similarly, the data information to be displayed on the page is also paid to the $ res_arr attribute in the paging class.
In this way, we can easily instantiate a paging class (fenyePage) and pass it through the parameter to the showorder () function, which completes the following actions:
① Information to be displayed on the page
② Total number of records in the table

The code is as follows:
/**
* Query the user's historical order records based on the specified user ID.
*
* @ Author Xiaofei 2012/5/30
* @ Param $ id user id
* @ Param $ fenye refers to an object instantiated to process paging.
* @ Todo $ sql1 statement "select * from table where * limit" this SQL statement is mainly used to retrieve data in the database and display it at the view layer.
* @ Todo $ sql2 statement "select count (id) from table" this SQL statement is used to obtain the total data volume
*/
Public function showorder ($ id = null, $ fenye = null)
{
$ Db = $ this-> getAdapter ();
$ Select = $ db-> select ();
$ Select-> from (array ('O' => 'order'), array ('o. ID', 'O. user_id ', 'O. user_name ', 'O. food_name', 'O. food_price ', 'O. order_time ', 'O. order_state '));
If ($ id! = Null ){
$ Select-> where ('o. user_id =? ', $ Id );
}
$ Select-> join (array ('D' => 'Department '), 'O. dep_id = d. ID', 'D. dep_name ');
If ($ fenye! = Null ){
$ Select-> limit ($ fenye-> pageSize, ($ fenye-> pageNow-1) * $ fenye-> pageSize );
}
$ Sql1 = $ select->__ toString ();
// This SQL statement is mainly used to calculate the total data volume.
$ Sql2 = "select count (id) FROM 'Order "';
$ Fenye-> res_arr = $ db-> fetchAll ($ sql1); // the data to be displayed is stored in the $ res_arr attribute of the paging class for convenient calling.
$ RowCount = $ db-> fetchAll ($ sql2); // Save the total data volume in the table to the rowCount attribute of the paging class.
$ Fenye-> rowCount = $ rowCount [0] ['count (id) '];
$ Fenye-> getLink ();
Return $ fenye-> res_arr;
}

Summarize the core code of paging.

The code is as follows:
$ This-> pageCount = ceil ($ this-> rowCount/$ this-> pageSize ));
$ Step = floor ($ this-> pageNow-1)/10) * 10 + 1;

Then select * from table where * limit in SQL is the $ select-> limit ($ fenye-> pageSize, ($ fenye-> pageNow-1) in this instance) * $ fenye-> pageSize.

Success ,...

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.