For php paging ideas and the use of ZF, if you need a friend, you can refer to it and get only 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;
}
Now, the paging class function has been implemented.
Original article: WEB development _ Xiaofei