欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php分页类与分页函数多种分页样式供选择

程序员文章站 2022-05-12 22:05:49
...
  1. /**

  2. * 页面名称:page_class.php
  3. */
  4. class Page {
  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. /**
  13. *
  14. * __construct是SubPages的构造函数,用来在创建类的时候自动运行.
  15. * @$each_disNums 每页显示的条目数
  16. * @nums 总条目数
  17. * @current_num 当前被选中的页
  18. * @sub_pages 每次显示的页数
  19. * @subPage_link 每个分页的链接
  20. * @subPage_type 显示分页的类型
  21. *
  22. * 当@subPage_type=1的时候为普通分页模式
  23. * example:共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  24. * 当@subPage_type=2的时候为经典分页样式
  25. * example:当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  26. */
  27. function Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) {
  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. }
  39. /**
  40. * 照顾低版本
  41. */
  42. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
  43. $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link);
  44. }
  45. /*

  46. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  47. */
  48. function __destruct() {
  49. unset ($each_disNums);
  50. unset ($nums);
  51. unset ($current_page);
  52. unset ($sub_pages);
  53. unset ($pageNums);
  54. unset ($page_array);
  55. unset ($subPage_link);
  56. }
  57. /*

  58. 用来给建立分页的数组初始化的函数。
  59. */
  60. function initArray() {
  61. for ($i = 0; $i sub_pages; $i++) {
  62. $this->page_array[$i] = $i;
  63. }
  64. return $this->page_array;
  65. }
  66. /*

  67. construct_num_Page该函数使用来构造显示的条目
  68. 即使:[1][2][3][4][5][6][7][8][9][10]
  69. */
  70. function construct_num_Page() {
  71. if ($this->pageNums sub_pages) {
  72. $current_array = array ();
  73. for ($i = 0; $i pageNums; $i++) {
  74. $current_array[$i] = $i +1;
  75. }
  76. } else {
  77. $current_array = $this->initArray();
  78. if ($this->current_page for ($i = 0; $i $current_array[$i] = $i +1;
  79. }
  80. }
  81. elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  82. for ($i = 0; $i $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  83. }
  84. } else {
  85. for ($i = 0; $i $current_array[$i] = $this->current_page - 2 + $i;
  86. }
  87. }
  88. }
  89. return $current_array;

  90. }
  91. /*

  92. 构造普通模式的分页
  93. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  94. */
  95. function subPageCss1() {
  96. $subPageCss1Str = "";
  97. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  98. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  99. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  100. if ($this->current_page > 1) {
  101. $firstPageUrl = $this->subPage_link . "1";
  102. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  103. $subPageCss1Str .= "[首页] ";
  104. $subPageCss1Str .= "[上一页] ";
  105. } else {
  106. $subPageCss1Str .= "[首页] ";
  107. $subPageCss1Str .= "[上一页] ";
  108. }
  109. if ($this->current_page pageNums) {

  110. $lastPageUrl = $this->subPage_link . $this->pageNums;
  111. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  112. $subPageCss1Str .= " [下一页] ";
  113. $subPageCss1Str .= "[尾页] ";
  114. } else {
  115. $subPageCss1Str .= "[下一页] ";
  116. $subPageCss1Str .= "[尾页] ";
  117. }
  118. return $subPageCss1Str;

  119. }

  120. /*

  121. 构造经典模式的分页
  122. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  123. */
  124. function subPageCss2() {
  125. $subPageCss2Str = "";
  126. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  127. if ($this->current_page > 1) {

  128. $firstPageUrl = $this->subPage_link . "1";
  129. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  130. $subPageCss2Str .= "[首页] ";
  131. $subPageCss2Str .= "[上一页] ";
  132. } else {
  133. $subPageCss2Str .= "[首页] ";
  134. $subPageCss2Str .= "[上一页] ";
  135. }
  136. $a = $this->construct_num_Page();

  137. for ($i = 0; $i $s = $a[$i];
  138. if ($s == $this->current_page) {
  139. $subPageCss2Str .= "[" . $s . "]";
  140. } else {
  141. $url = $this->subPage_link . $s;
  142. $subPageCss2Str .= "[" . $s . "]";
  143. }
  144. }
  145. if ($this->current_page pageNums) {

  146. $lastPageUrl = $this->subPage_link . $this->pageNums;
  147. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  148. $subPageCss2Str .= " [下一页] ";
  149. $subPageCss2Str .= "[尾页] ";
  150. } else {
  151. $subPageCss2Str .= "[下一页] ";
  152. $subPageCss2Str .= "[尾页] ";
  153. }
  154. return $subPageCss2Str;
  155. }
  156. }
  157. //测试两种不同效果

  158. $t = new Page(10, 100, $_GET['p'], 5, 'page_class.php?p=');
  159. echo $t->subPageCss2(); //调用经典分页函数
  160. echo "
    ";

  161. echo $t->subPageCss1();//调用普通分页函数
复制代码