- $ Nav = ''; // A variable used to save the number of pages
- For ($ I = 1; $ I <= 13; $ I ++)
- {
- $ Nav. = "page". $ I ";
- }
The above for loop will output 1st pages, 2nd pages, 3rd pages, 4th pages, 5th pages, 6th pages, 7th pages, 8th pages, 9th pages, 10th pages, 11th pages, pages, page 12th, page 13th if we only want to display 10 pages at a time? For example, on pages 1-10 and pages 11-20, you can 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 current page $ pageNow in 11 ~ Between 20, then $ step = 10 on the current page $ pageNow in 21 ~ Between 30, then $ step = 20 refer to the specific implementation process code, we do not have to find, the second condition of the for loop only requires 10 values to display only 10 values at a time. we will package this step in the getLink () method of the fenyePage class () 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.
- /**
- * $ 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 ";
- }
- }
- ?>
Because the task of operating databases in zf is 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 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. The data information to be displayed on the page is also paid to the $ res_arr attribute in the paging class, we can easily instantiate a paging class (fenyePage) and pass it through the parameter to the showorder () function to complete the following actions: ① information to be displayed on the page ② total number of records in the table
- /**
- * 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
- * @ Link http://bbs.it-home.org
- */
- 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
- }
|