Paging ideas and use in ZF. Paging method: you only need to get two variables, and the result is half done: 1. the number of records to be displayed on each page $ pageSize2. the total data volume in the table $ rowCount has the preceding two variables, so we can use the paging logic:
You only need to get two variables to get half done:
1. number of records to be displayed per page $ pageSize
2. 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.
$ 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.
$ 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.
1 2 /**
3 * $ SQL statement: ① get data ② get the total number of records
4 */
5 class fenyePage {
6 public $ pageSize = 5; // The number displayed on each page --> The number specified by the programmer
7 public $ rowCount; // This is obtained FROM the database (such as select count (id) from table) to save the total number of records
8 public $ pageNow; // obtained through $ _ GET ['Page'] to save the current page number.
9 public $ pageCount; // calculated to save the total number of pages
10 public $ res_arr; // used to save the data to be displayed on the page (for example, save the data retrieved from table limit)
11 public $ nav; // display the page number of the navigation bar
12
13 /**
14 * obtain the hyperlink of the current page
15 *
16 * @ author Xiaofei
17 */
18 public function getLink ()
19 {
20 $ this-> nav = '';
21 $ this-> pageCount = ceil ($ this-> rowCount/$ this-> pageSize ));
22 $ step = floor ($ this-> pageNow-1)/10) * 10 + 1;
23 if ($ this-> pageNow> 10)
24 {
25 $ this-> nav. = "<"; // forward each 10 pages
26}
27 if ($ this-> pageNow! = 1)
28 {
29 $ this-> nav. = "pageNow-1)." '> Previous Page ";
30}
31 if ($ this-> pageNow! = 1)
32 {
33 $ this-> nav. = "homepage ";
34}
35 for ($ start = $ step; $ start <$ step + 10 & $ start <= $ this-> pageCount; $ start ++)
36 {
37 $ this-> nav. = "". $ start ."";
38}
39 if ($ this-> pageNow! = $ This-> pageCount)
40 {
41 $ this-> nav. = "pageCount." '> last page ";
42}
43 if ($ this-> pageNow! = $ This-> pageCount)
44 {
45 $ this-> nav. = "pageNow + 1)." '> next page ";
46}
47 if ($ this-> pageCount> 10 & $ this-> pageNow <$ this-> pageCount-8 ){
48 $ this-> nav. = ">"; // flip back every 10 pages
49}
50 $ this-> nav. = "/total". $ this-> pageCount. "page ";
51}
52}
53?>
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
1 /**
2 * query the user's historical order records based on the specified user ID
3 *
4 * @ author Xiaofei 2012/5/30
5 * @ param $ id user id
6 * @ param $ fenye: an object instantiated to process pagination
7 * @ 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.
8 * @ todo $ sql2 statement "select count (id) from table" this SQL statement is used to obtain the total data volume
9 */
10 public function showorder ($ id = null, $ fenye = null)
11 {
12 $ db = $ this-> getAdapter ();
13
14 $ select = $ db-> select ();
15 $ 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 '));
16 if ($ id! = Null ){
17 $ select-> where ('o. user_id =? ', $ Id );
18}
19 $ select-> join (array ('D' => 'Department '), 'O. dep_id = d. ID', 'D. dep_name ');
20 if ($ fenye! = Null ){
21 $ select-> limit ($ fenye-> pageSize, ($ fenye-> pageNow-1) * $ fenye-> pageSize );
22}
23 $ sql1 = $ select->__ toString ();
24 // This SQL statement is mainly used to calculate the total data volume
25 $ sql2 = "select count (id) FROM 'Order "';
26 $ 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.
27 $ rowCount = $ db-> fetchAll ($ sql2); // Save the total data volume in the table to the rowCount attribute of the paging class.
28 $ fenye-> rowCount = $ rowCount [0] ['count (id) '];
29 $ fenye-> getLink ();
30 return $ fenye-> res_arr;
31}
Now, the paging class function has been implemented.
From WEB Development _ Xiaofei
Half of the success is achieved by getting two variables: 1. number of records to be displayed per page $ pageSize 2. total data volume in the table $ rowCount has the above two variables, we can get...