Paging ideas:
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.
$ Nav = ''; // a variable used to save the number of pages
For ($ I = 1; $ I <= 13; $ I ++)
{
$ Nav. = "<a href = 'index. php? Page = ". $ I." '> page ". $ I." </a> ";
}
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. = "<a href = 'index. php? Page = ". $ I." '> page ". $ I." </a> ";
}
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
Refer to the specific implementation processCodeWe can easily find that the second condition of the For Loop only requires 10 to show 10 at a time. We will package this step in the fenyepage class.Getlink ()Method
Then again, how can we get the values of the $ pagesize and $ rowcount variables?
$ PagesizeProgramYou can specify $ rowcount by using a simple function that executes SQL statements.
1 <? PHP 2 /* * 3 * $ SQL statement: ① obtain data ② obtain the total number of records 4 */ 5 Class Fenyepage { 6 Public $ Pagesize = 5; // Number of entries displayed per 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, saving the data retrieved from Table limit) 11 Public $ Nav ; // Display the navigation bar on the page 12 13 /* * 14 * Obtain the hyperlink of the current page 15 * 16 * @ Author Xiaofei 2012/5/30 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. = "<a href = 'index. php? Page = ".( $ Step -1). "'> </a> "; // Forward every 10 pages 26 } 27 If ( $ This -> Pagenow! = 1 ) 28 { 29 $ This -> Nav. = "<a href = 'index. php? Page = ".( $ This -> PageNow-1). "'> previous </a>" ; 30 } 31 If ( $ This -> Pagenow! = 1 ) 32 { 33 $ This -> Nav. = "<a href = 'index. php? Page = 1'> homepage </a>" ; 34 } 35 For ( $ Start = $ Step ; $ Start < $ Step + 10 &&$ Start <= $ This -> Pagecount; $ Start ++ ) 36 { 37 $ This -> Nav. = "<a href = 'index. php? Page = ". $ Start . "'> ". $ Start . "</A>" ; 38 } 39 If ( $ This -> Pagenow! = $ This -> Pagecount) 40 { 41 $ This -> Nav. = "<a href = 'index. php? Page = ". $ This -> Pagecount. "'> last page </a>" ; 42 } 43 If ($ This -> Pagenow! = $ This -> Pagecount) 44 { 45 $ This -> Nav. = "<a href = 'index. php? Page = ".( $ This -> Pagenow + 1). "'> next page </a>" ; 46 } 47 If ( $ This -> Pagecount> 10 &&$ This -> Pagenow < $ This -> PageCount-8 ){ 48 $ This -> Nav. = "<a href = 'index. php? Page = ".( $ Step + 10). "'>>></A> "; // Turning 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 refers to an object instantiated to process paging. 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 ); // Saves 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.
OriginalArticle:Web development _ Xiaofei
Reprinted please indicate the source:Paging ideas and use in ZF