Example of combining php paging code with smarty

Source: Internet
Author: User
Example of combining php paging code with smarty

  1. /**
  2. * Paging class
  3. */
  4. Class Pager {
  5. Var $ total; // The total number of records.
  6. Var $ pageSize; // number of records displayed on each page
  7. Var $ currentPage; // The current page number.
  8. Var $ offset; // record offset
  9. Var $ pageTotal; // The total number of pages.
  10. Var $ numberOffset = 5; // page offset
  11. Var $ request = ""; // page request parameters
  12. // ==============================
  13. // Fn: Pager
  14. // Function: constructor
  15. // ==============================
  16. Public function _ construct ($ total, $ pageSize, $ currentPage, $ request = ""){
  17. $ This-> total = $ total;
  18. $ This-> pageSize = $ pageSize;
  19. $ This-> pageOffset ();
  20. $ This-> pageTotal ();
  21. $ This-> currentPage ($ currentPage );
  22. $ This-> request = $ request;
  23. }
  24. // ==============================
  25. // Fn: pageOffset
  26. // Function: database record offset
  27. // ==============================
  28. Public function pageOffset (){
  29. Return $ this-> offset = $ this-> pageSize * ($ this-> currentPage-1 );
  30. }
  31. // ==============================
  32. // Fn: pageTotal
  33. // Function: calculate the total number of pages
  34. // ==============================
  35. Public function pageTotal (){
  36. Return $ this-> pageTotal = ceil ($ this-> total/$ this-> pageSize );
  37. }
  38. // ==============================
  39. // Fn: currentPage
  40. // Function: set the page number
  41. // ==============================
  42. Public function currentPage ($ currentPage ){
  43. If (isset ($ currentPage )){
  44. $ This-> currentPage = intval ($ currentPage );
  45. } Else {
  46. $ This-> currentPage = 1;
  47. }
  48. Return $ this-> currentPage;
  49. }
  50. // ==============================
  51. // Fn: nextPage
  52. // Function: Jump to the next page
  53. // ==============================
  54. Public function nextPage (){
  55. // Display the number of records
  56. $ Link = "total {$ this-> total} items ";
  57. // Page number step size
  58. $ StepPage = $ this-> currentPage? Ceil ($ this-> currentPage/$ this-> numberOffset): 1;
  59. // Set the number page number
  60. $ NumberPage = ($ this-> pageTotal> $ this-> numberOffset )? $ This-> numberOffset: $ this-> pageTotal;
  61. // Only one page
  62. If ($ this-> total <= $ this-> pageSize ){
  63. $ Link. = "[Homepage] | [last page]";
  64. } Else {
  65. // Set the total number of pages and the current page
  66. $ Link. = "page {$ this-> currentPage}/{$ this-> pageTotal ";
  67. // Homepage
  68. $ Link. = "request}> [Homepage]";
  69. // Next column
  70. If ($ stepPage> 1 ){
  71. $ LastIndex = ($ stepPage-1) * $ this-> numberOffset;
  72. $ Link. = "request}> [<]";
  73. }
  74. // Previous Page
  75. If ($ this-> currentPage> 1 ){
  76. $ PrePage = $ this-> currentPage-1;
  77. $ Link. = "request}> [<]";
  78. }
  79. // Number page
  80. $ I = ($ stepPage-1) * $ this-> numberOffset;
  81. For ($ j = $ I; $ j <($ I + $ numberPage) & $ j <$ this-> pageTotal; $ j ++ ){
  82. $ NewPage = $ j + 1;
  83. If ($ this-> currentPage ==$ j + 1 ){
  84. $ Link. ="[". ($ J + 1)."]";
  85. } Else {
  86. $ Link. = "request}> [". ($ j + 1). "]";
  87. }
  88. }
  89. // Next page
  90. If ($ this-> currentPage <$ this-> pageTotal ){
  91. $ NextPage = $ this-> currentPage + 1;
  92. $ Link. = "request}> [>]";
  93. }
  94. // Next column
  95. If ($ stepPage <$ this-> total ){
  96. $ NextPage = $ stepPage * ($ this-> numberOffset + 1 );
  97. If ($ nextPage <$ this-> pageTotal ){
  98. $ Link. = "request}> [>]";
  99. }
  100. }
  101. // Last Page
  102. If ($ this-> currentPage <$ this-> pageTotal ){
  103. $ Link. = "... pageTotal }{$ this-> request}> [last page]";
  104. }
  105. }
  106. Return $ link;
  107. }
  108. }
  109. ?>

II. php paging call example:

1. get the page pages returned from the URL:

  1. $ Cur_page = 1;
  2. If (isset ($ _ GET ["pageNo"]) {
  3. $ Cur_page = $ _ GET ["pageNo"];
  4. }

2. create a paging object:

  1. $ Nums: total number of data records
  2. $ Page_size: Number of Entries displayed per page
  3. $ Cur_page: current page number
  4. $ Request: optional parameters for other Url requests
  5. $ Pager = new Pager ($ nums, $ page_size, $ cur_page, $ request );

3. smarty value assignment:

  1. $ Show = get the data to be displayed
  2. $ This-> tpl-> assign ('numlink', $ pager-> nextPage (); // Obtain the page List
  3. $ This-> tpl-> assign ('data', $ show );

Paging effect:

The above paging code does not implement url redirection, so that all transmitted information in the address bar is exposed, and you can complete it.

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.