Php paging instance php and mysql simple paging code

Source: Internet
Author: User
Php paging instance php and mysql simple paging code

  1. Class PageModel {
  2. /**
  3. * Retrieve the paging array
  4. * @ Param unknown $ current page count
  5. * @ Param unknown $ goodsCount total number of items
  6. * @ Param unknown $ pageLength: number of displayed pages per page
  7. */
  8. Public static function getPageArr ($ page, $ goodsCount, $ pageCountLength, $ pageLength ){
  9. // Total number of pages
  10. $ AllPageCount = ceil ($ goodsCount/$ pageLength );
  11. // If the page is always shorter than the length, set the page length to the total number of pages
  12. If ($ allPageCount <= $ pageCountLength ){
  13. $ AllPageCount = ceil ($ goodsCount/$ pageLength );
  14. }
  15. // The total number of pages displayed on one page
  16. If ($ allPageCount <= $ pageCountLength ){
  17. For ($ I = 0; $ I <$ allPageCount; $ I ++ ){
  18. $ Arr [] = array ('page' => $ I + 1 );
  19. }
  20. Return $ arr;
  21. }
  22. // Length before and after
  23. $ HalfLength = floor ($ pageCountLength/2 );
  24. // Because it is too small, put it in the original position, left
  25. If ($ page <= $ halfLength ){
  26. $ Arr = array ();
  27. For ($ I = 0; $ I <$ pageCountLength; $ I ++ ){
  28. $ Arr [] = array ('page' => $ I + 1 );
  29. }
  30. Return $ arr;
  31. }
  32. // The value is too large. only the edge is obtained. if the value is exceeded, only the edge is obtained.
  33. If ($ page> $ allPageCount-floor ($ pageCountLength/2 )){
  34. For ($ I =-$ pageCountLength; $ I <0; $ I ++ ){
  35. $ Arr [] = array ('page' => $ allPageCount + $ I + 1 );
  36. }
  37. Return $ arr;
  38. }
  39. // The number in the middle.
  40. For ($ I =-$ halfLength; $ I <$ pageCountLength-$ halfLength; $ I ++ ){
  41. $ Arr [] = array ('page' => $ page + $ I );
  42. }
  43. Return $ arr;
  44. }
  45. }

II. php paging code

Code:

  1. Class Helper_Page {

  2. /** Total information count */

  3. Var $ infoCount;
  4. /** Total Page number */
  5. Var $ pageCount;
  6. /** Number of Entries displayed per page */
  7. Var $ items;
  8. /** Current page number */
  9. Var $ pageNo;
  10. /** Start position of the query */
  11. Var $ startPos;
  12. /** Next page */
  13. Var $ nextPageNo;
  14. /** Previous page */
  15. Var $ prevPageNo;

  16. Function Helper_Page ($ infoCount, $ items, $ pageNo)

  17. {
  18. $ This-> infoCount = $ infoCount;
  19. $ This-> items = $ items;
  20. $ This-> pageNo = $ pageNo;
  21. $ This-> pageCount = $ this-> GetPageCount ();
  22. $ This-> AdjustPageNo ();
  23. $ This-> startPos = $ this-> GetStartPos ();
  24. }
  25. Function AdjustPageNo ()
  26. {
  27. If ($ this-> pageNo = ''| $ this-> pageNo <1)
  28. $ This-> pageNo = 1;
  29. If ($ this-> pageNo> $ this-> pageCount)
  30. $ This-> pageNo = $ this-> pageCount;
  31. }
  32. /**
  33. * Next page
  34. */
  35. Function GoToNextPage ()
  36. {
  37. $ NextPageNo = $ this-> pageNo + 1;
  38. If ($ nextPageNo> $ this-> pageCount)
  39. {
  40. $ This-> nextPageNo = $ this-> pageCount;
  41. Return false;
  42. }
  43. $ This-> nextPageNo = $ nextPageNo;
  44. Return true;
  45. }
  46. /**
  47. * Previous page
  48. */
  49. Function GotoPrevPage ()
  50. {
  51. $ PrevPageNo = $ this-> pageNo-1;
  52. If ($ prevPageNo <1)
  53. {
  54. $ This-> prevPageNo = 1;
  55. Return false;
  56. }
  57. $ This-> prevPageNo = $ prevPageNo;
  58. Return true;
  59. }
  60. Function GetPageCount ()
  61. {
  62. Return ceil ($ this-> infoCount/$ this-> items );
  63. }
  64. Function GetStartPos ()
  65. {
  66. Return ($ this-> pageNo-1) * $ this-> items;
  67. }
  68. }

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.