Paging.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Services;
  3. class Paging
  4. {
  5. private $each_disNums; //每页显示的条目数
  6. private $nums; //总条目数
  7. private $current_page; //当前被选中的页
  8. private $sub_pages; //每次显示的页数
  9. private $pageNums; //总页数
  10. private $page_array = array(); //用来构造分页的数组
  11. private $subPage_link; //每个分页的链接
  12. private $subPage_type; //显示分页的类型
  13. /*
  14. construct是SubPages的构造函数,用来在创建类的时候自动运行.
  15. @$each_disNums 每页显示的条目数 自定义一个变量赋值为页数:$page = 2;
  16. @nums 总条目数 (统计表中要查找的记录数)
  17. @current_num 当前被选中的页(在subpage_link这个参数中传输一个值:Messageshow.php?cur_page=,在接收页面接收)
  18. @sub_pages 每次显示的页数(每次显示的页数=总条目数/每页显示的条目数)
  19. @subPage_link 每个分页的链接(显示这些记录的页面)
  20. @subPage_type 显示分页的类型
  21. 当@subPage_type=1的时候为普通分页模式
  22. example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  23. 当@subPage_type=2的时候为经典分页样式
  24. example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  25. */
  26. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_link, $subPage_type = 2)
  27. {
  28. $this->each_disNums = intval($each_disNums);
  29. $this->nums = intval($nums);
  30. if (!$current_page) {
  31. $this->current_page = 1;
  32. } else {
  33. $this->current_page = intval($current_page);
  34. }
  35. $this->sub_pages = intval($sub_pages);
  36. $this->pageNums = ceil($nums / $each_disNums);
  37. $this->subPage_link = $subPage_link;
  38. //$this->show_SubPages($subPage_type);
  39. }
  40. /*
  41. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  42. */
  43. function __destruct()
  44. {
  45. //释放变量
  46. unset($each_disNums);
  47. unset($nums);
  48. unset($current_page);
  49. unset($sub_pages);
  50. unset($pageNums);
  51. unset($page_array);
  52. unset($subPage_link);
  53. unset($subPage_type);
  54. }
  55. /*
  56. show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
  57. */
  58. function show_SubPages($subPage_type)
  59. {
  60. if ($subPage_type == 1) {
  61. $this->subPageCss1();
  62. } elseif ($subPage_type == 2) {
  63. $this->subPageCss2();
  64. }
  65. }
  66. /* 用来给建立分页的数组初始化的函数。
  67. */
  68. function initArray()
  69. {
  70. for ($i = 0; $i < $this->sub_pages; $i++) {
  71. $this->page_array[$i] = $i;
  72. }
  73. return $this->page_array;
  74. }
  75. /* construct_num_Page该函数使用来构造显示的条目
  76. * 即使:[1][2][3][4][5][6][7][8][9][10]
  77. */
  78. function construct_num_Page()
  79. {
  80. if ($this->pageNums < $this->sub_pages) {
  81. $current_array = array();
  82. for ($i = 0; $i < $this->pageNums; $i++) {
  83. $current_array[$i] = $i + 1;
  84. }
  85. } else {
  86. $current_array = $this->initArray();
  87. if ($this->current_page <= 3) {
  88. for ($i = 0; $i < count($current_array); $i++) {
  89. $current_array[$i] = $i + 1;
  90. }
  91. } elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  92. for ($i = 0; $i < count($current_array); $i++) {
  93. $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  94. }
  95. } else {
  96. for ($i = 0; $i < count($current_array); $i++) {
  97. $current_array[$i] = $this->current_page - 2 + $i;
  98. }
  99. }
  100. }
  101. return $current_array;
  102. }
  103. /* 构造普通模式的分页
  104. * 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  105. *
  106. */
  107. function subPageCss1()
  108. {
  109. $subPageCss1Str = "";
  110. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  111. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  112. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  113. if ($this->current_page > 1) {
  114. $firstPageUrl = $this->subPage_link . "1";
  115. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  116. $subPageCss1Str .= "[<a href='$firstPageUrl'>首页</a>] ";
  117. $subPageCss1Str .= "[<a href='$prewPageUrl'>上一页</a>] ";
  118. } else {
  119. $subPageCss1Str .= "[首页] ";
  120. $subPageCss1Str .= "[上一页] ";
  121. }
  122. if ($this->current_page < $this->pageNums) {
  123. $lastPageUrl = $this->subPage_link . $this->pageNums;
  124. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  125. $subPageCss1Str .= " [<a href='$nextPageUrl'>下一页</a>] ";
  126. $subPageCss1Str .= "[<a href='$lastPageUrl'>尾页</a>] ";
  127. } else {
  128. $subPageCss1Str .= "[下一页] ";
  129. $subPageCss1Str .= "[尾页] ";
  130. }
  131. echo $subPageCss1Str;
  132. }
  133. /*
  134. * 构造经典模式的分页
  135. * 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  136. */
  137. public function subPageCss2()
  138. {
  139. $subPageCss2Str = "共有" . $this->nums . "条记录 ";
  140. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  141. if ($this->nums > 0) {
  142. if ($this->current_page > 1) {
  143. $firstPageUrl = $this->subPage_link . "1";
  144. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  145. $subPageCss2Str .= "[<a href='$firstPageUrl'>首页</a>] ";
  146. $subPageCss2Str .= "[<a href='$prewPageUrl'>上一页</a>] ";
  147. } else {
  148. $subPageCss2Str .= "[首页] ";
  149. $subPageCss2Str .= "[上一页] ";
  150. }
  151. $a = $this->construct_num_Page();
  152. for ($i = 0; $i < count($a); $i++) {
  153. $s = $a[$i];
  154. if ($s == $this->current_page) {
  155. $subPageCss2Str .= "[<span style='color:red;font-weight:bold;'>" . $s . "</span>]";
  156. } else {
  157. $url = $this->subPage_link . $s;
  158. $subPageCss2Str .= "[<a href='$url'>" . $s . "</a>]";
  159. }
  160. }
  161. if ($this->current_page < $this->pageNums) {
  162. $lastPageUrl = $this->subPage_link . $this->pageNums;
  163. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  164. $subPageCss2Str .= " [<a href='$nextPageUrl'>下一页</a>] ";
  165. $subPageCss2Str .= "[<a href='$lastPageUrl'>尾页</a>] ";
  166. } else {
  167. $subPageCss2Str .= "[下一页] ";
  168. $subPageCss2Str .= "[尾页] ";
  169. }
  170. }
  171. return $subPageCss2Str;
  172. }
  173. }