Php paging code example and use in ZF

Source: Internet
Author: User
Php paging code example and use in ZF

  1. $ Nav = ''; // A variable used to save the number of pages
  2. For ($ I = 1; $ I <= 13; $ I ++)
  3. {
  4. $ Nav. = "page". $ I ";
  5. }

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:

  1. $ Step = floor ($ pageNow-1)/10) * 10 + 1;
  2. For ($ I = $ step; $ I <= $ step + 10; $ I ++)
  3. {
  4. $ Nav. = "page". $ I ";
  5. }

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.

  1. /**
  2. * $ SQL statement: ① obtain data ② obtain the total number of records
  3. */
  4. Class fenyePage {
  5. Public $ pageSize = 5; // The number displayed on each page --> The number specified by the programmer
  6. Public $ rowCount; // This is obtained FROM the database (such as select count (id) from table) to save the total number of records
  7. Public $ pageNow; // obtained through $ _ GET ['Page'] to save the current page number.
  8. Public $ pageCount; // calculated to save the total number of pages
  9. Public $ res_arr; // used to save the data to be displayed on the page (for example, save the data retrieved from table limit)
  10. Public $ nav; // display the navigation bar on the page
  11. /**
  12. * Obtain the hyperlink of the current page
  13. *
  14. * @ Author Xiaofei 2012/5/30
  15. */
  16. Public function getLink ()
  17. {
  18. $ This-> nav = '';
  19. $ This-> pageCount = ceil ($ this-> rowCount/$ this-> pageSize ));
  20. $ Step = floor ($ this-> pageNow-1)/10) * 10 + 1;
  21. If ($ this-> pageNow> 10)
  22. {
  23. $ This-> nav. = "<"; // forward each 10 pages
  24. }
  25. If ($ this-> pageNow! = 1)
  26. {
  27. $ This-> nav. = "pageNow-1)." '> Previous Page ";
  28. }
  29. If ($ this-> pageNow! = 1)
  30. {
  31. $ This-> nav. = "homepage ";
  32. }
  33. For ($ start = $ step; $ start <$ step + 10 & $ start <= $ this-> pageCount; $ start ++)
  34. {
  35. $ This-> nav. = "". $ start ."";
  36. }
  37. If ($ this-> pageNow! = $ This-> pageCount)
  38. {
  39. $ This-> nav. = "pageCount." '> last page ";
  40. }
  41. If ($ this-> pageNow! = $ This-> pageCount)
  42. {
  43. $ This-> nav. = "pageNow + 1)." '> next page ";
  44. }
  45. If ($ this-> pageCount> 10 & $ this-> pageNow <$ this-> pageCount-8 ){
  46. $ This-> nav. = ">"; // returns the result of every 10 pages.
  47. }
  48. $ This-> nav. = "/total". $ this-> pageCount. "page ";
  49. }
  50. }
  51. ?>

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

  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. * @ Link http://bbs.it-home.org
  10. */
  11. Public function showorder ($ id = null, $ fenye = null)
  12. {
  13. $ Db = $ this-> getAdapter ();
  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. }

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.