当前位置:Gxlcms > PHP教程 > php分页类与分页函数多种分页样式供选择

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

  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 < $this->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 < $this->sub_pages) {
  72. $current_array = array ();
  73. for ($i = 0; $i < $this->pageNums; $i++) {
  74. $current_array[$i] = $i +1;
  75. }
  76. } else {
  77. $current_array = $this->initArray();
  78. if ($this->current_page <= 3) {
  79. for ($i = 0; $i < count($current_array); $i++) {
  80. $current_array[$i] = $i +1;
  81. }
  82. }
  83. elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  84. for ($i = 0; $i < count($current_array); $i++) {
  85. $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  86. }
  87. } else {
  88. for ($i = 0; $i < count($current_array); $i++) {
  89. $current_array[$i] = $this->current_page - 2 + $i;
  90. }
  91. }
  92. }

  93. return $current_array;

  94. }

  95. /*

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

  113. if ($this->current_page < $this->pageNums) {

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

  122. return $subPageCss1Str;

  123. }

  124. /*

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

  131. if ($this->current_page > 1) {

  132. $firstPageUrl = $this->subPage_link . "1";
  133. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  134. $subPageCss2Str .= "[首页] ";
  135. $subPageCss2Str .= "[上一页] ";
  136. } else {
  137. $subPageCss2Str .= "[首页] ";
  138. $subPageCss2Str .= "[上一页] ";
  139. }

  140. $a = $this->construct_num_Page();

  141. for ($i = 0; $i < count($a); $i++) {
  142. $s = $a[$i];
  143. if ($s == $this->current_page) {
  144. $subPageCss2Str .= "[" . $s . "]";
  145. } else {
  146. $url = $this->subPage_link . $s;
  147. $subPageCss2Str .= "[" . $s . "]";
  148. }
  149. }

  150. if ($this->current_page < $this->pageNums) {

  151. $lastPageUrl = $this->subPage_link . $this->pageNums;
  152. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  153. $subPageCss2Str .= " [下一页] ";
  154. $subPageCss2Str .= "[尾页] ";
  155. } else {
  156. $subPageCss2Str .= "[下一页] ";
  157. $subPageCss2Str .= "[尾页] ";
  158. }
  159. return $subPageCss2Str;
  160. }
  161. }

  162. //测试两种不同效果

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

  165. echo "
    ";

  166. echo $t->subPageCss1();//调用普通分页函数

人气教程排行