php分页程序paginator程序代码

  1. <?php
  2. class paginator {
  3. function __construct () {
  4. }
  5. static function render($page_type, array $args = array()) {
  6. $html = '';
  7. switch ($page_type)
  8. {
  9. case '1':
  10. $html = page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v);
  11. break;
  12. case '2':
  13. $html = fpage($row_num, $pagesize, $page, $url);
  14. break;
  15. case '3':
  16. break;
  17. default:
  18. $html = page($num, $perpage, $curpage, $mpurl, $hiddenFrom='');
  19. }
  20. return $html;
  21. }
  22. //函数1:根据总页数,当前页,和页分组及url产生分页导航
  23. function page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v) {
  24. // 分页函数参数列表(有多少页,当前页,每页多少个 ,链接地址)
  25. $base_url .= strpos($base_url, '?') ? '&' : '?'; //如果有?,则添加&,否则加?
  26. $page_val = "";
  27. if ($page_num < 1) {
  28. return $page_val; //如果小于1,返回空
  29. } elseif ($page_num == 1) {
  30. return $page_val = 1; //如果等于1,返回1
  31. }
  32. //分组
  33. $group_ok = !(($page_num-1) % ($per_group-1)); //没有余数
  34. // 计算一共有多少组
  35. if($group_ok) {
  36. $group_num = ($page_num-1)/($per_group-1);
  37. }else {
  38. $group_num = ceil(($page_num-1) / ($per_group-1));
  39. } //判断当前页是否为一组的最后一个
  40. //组数
  41. $is_last_page = !(($cur_page-1) % ($per_group-1)); //计算当前在的组数
  42. if ($is_last_page) {
  43. $cur_group = ($cur_page-1) / ($per_group-1) + 1;
  44. } else {
  45. $cur_group = ceil(($cur_page-1) / ($per_group-1));
  46. }
  47. // 计算显示的起始页 ,这个关键
  48. $from = ($cur_group - 1) * ($per_group-1) + 1;
  49. $from = $cur_group <= 1 ? 1 :$from;
  50. $to = $from + $per_group;
  51. $to = $to > $page_num ? $page_num + 1 : $to;
  52. for($a = $from; $a < $to; $a++) {
  53. if ($a != $cur_page) {
  54. //不是当前页
  55. $page_val .= '<li><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $a . '">' . $a . '</a></li>';
  56. } else {
  57. //当前页
  58. $page_val .='<li class="current">'. $a .'</li>';
  59. }
  60. }
  61. //上一页下一页首页尾页
  62. $next = $cur_page + 1; //当前页+1
  63. $prev = $cur_page-1; //当前页-1
  64. if ($cur_page != $page_num) {
  65. //当前页不是最后一页
  66. $page_val .= '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $next . '">' . '下一页' . '</a></span>';
  67. $page_val .= '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $page_num . '">' . '>>' . '</a></span>'; //尾页
  68. }
  69. if ($cur_page != 1){
  70. //不是第一页,加上前面的内容
  71. $page_val = '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $prev . '">' . '上一页' . '</a></span>' . $page_val;
  72. $page_val = '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=1">' . '<<' . '</a></span>' . $page_val; //首页
  73. }
  74. $page_val = '<ul class="page_nav">'.$page_val.'</ul>';
  75. return $page_val;
  76. }
  77. // 函数2:根据记录数,页列清数,当前页,url年成分页
  78. function fpage($row_num, $pagesize, $page, $url) {
  79. //$page,当前页;$row_num记录总数;$pagesize:每页记录数;$url记录页;
  80. //如果,如果有作数,则取加1
  81. $page_count = $row_num % $pagesize?intval($row_num / $pagesize) + 1:intval($row_num / $pagesize);
  82. if ($page_count == 1) {
  83. $page_string = "第一页|上一页|下一页|尾页";
  84. } else {
  85. //如果不止一页
  86. if ($page == 1) {
  87. //第一页显示
  88. $page_string = " <a href=" . $url . "?page=1>第一页 </a>|上一页| <a href=" . $url . "?page=" . ($page + 1) . ">下一页 </a>| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
  89. }
  90. if (($page > 1) and ($page < $page_count)) {
  91. $page_string = " <a href=" . $url . "?page=1>第一页 </a>| <a href=" . $url . "?page=" . ($page-1) . ">上一页 </a>| <a href=" . $url . "?page=" .
  92. ($page + 1) . ">下一页 </a>| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
  93. }
  94. if ($page == $page_count) {
  95. $page_string = " <a href=" . $url . "?page=1>第一页 </a>| <a href=" . $url . "?page=" . ($page-1) . ">上一页 </a>|下一页| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
  96. }
  97. }
  98. $page_str = $page_string . ("," . $page . "/" . $page_count . "页");
  99. echo " <br> <p align=center>" . $page_str . " </p>";
  100. }
  101. /**
  102. * 分页函数
  103. *
  104. * @param int $num:记录总数
  105. * @param int $perpage:每页记录
  106. * @param int $curpage:当前页
  107. * @param string $mpurl:路径url
  108. * @param unknown_type $hiddenFrom
  109. * @return string
  110. */
  111. function page($num, $perpage, $curpage, $mpurl,$hiddenFrom='') {
  112. $multipage = '';
  113. //恢复
  114. $mpurl .= strpos($mpurl, '?') ? '&' : '?';
  115. if($num > $perpage) {
  116. $page = 10;
  117. $offset = 2;
  118. $pages = @ceil($num / $perpage); //总页数
  119. if($page > $pages) {
  120. $from = 1;
  121. $to = $pages;
  122. } else {
  123. //如果总页数>10
  124. $from = $curpage - $offset; //当前页-2
  125. $to = $from + $page - 1; //当前页+10-3
  126. if($from < 1) {
  127. $to = $curpage + 1 - $from;
  128. $from = 1;
  129. if($to - $from < $page) {
  130. $to = $page;
  131. }
  132. } elseif($to > $pages) {
  133. $from = $pages - $page + 1;
  134. $to = $pages;
  135. }
  136. }
  137. if ($hiddenFrom) {
  138. $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="#" '.js_page($hiddenFrom,1).'>首页</a> ' : '').
  139. ($curpage > 1 ? ' <a href="#" '.js_page($hiddenFrom,$curpage - 1).'>‹‹上一页</a> ' : '');
  140. for($i = $from; $i <= $to; $i++) {
  141. $multipage .= $i == $curpage ? '<span>'.$i.'</span> ' :
  142. '<a href="#" '.js_page($hiddenFrom,$i).'>'.$i.'</a> ';
  143. }
  144. $multipage .= ($curpage < $pages ? '<a href="#" '.js_page($hiddenFrom,$curpage + 1).'>下一页››</a>' : '').
  145. ($to < $pages ? '<a href="#" '.js_page($hiddenFrom,$pages).'> 末页</a>' : '');
  146. $multipage = $multipage ? '<div class="list-page">'.$multipage.'</div>' : '';
  147. } else {
  148. $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1">首页</a> ' : '').
  149. ($curpage > 1 ? ' <a href="'.$mpurl.'page='.($curpage - 1).'">‹‹上一页</a> ' : '');
  150. for($i = $from; $i <= $to; $i++) {
  151. $multipage .= $i == $curpage ? '<span>'.$i.'</span> ' :
  152. '<a href="'.$mpurl.'page='.$i.'">'.$i.'</a> ';
  153. }
  154. $multipage .= ($curpage < $pages ? '<a href="'.$mpurl.'page='.($curpage + 1).'">下一页››</a>' : '').
  155. ($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'"> 末页</a>' : '');
  156. $multipage = $multipage ? '<div class="list-page">'.$multipage.'</div>' : '';
  157. }
  158. }
  159. return $multipage;
  160. }
  161. //暂不用
  162. function page_get_start($page, $ppp, $totalnum) {
  163. $totalpage = ceil($totalnum / $ppp);
  164. $page = max(1, min($totalpage, intval($page)));
  165. return ($page - 1) * $ppp;
  166. }
  167. //暂不用
  168. function js_page($hiddenFrom,$page)
  169. {
  170. if ($hiddenFrom) {
  171. return ' onclick=javascript:$("#page").val("'.$page.'");$("#'.$hiddenFrom.'").submit(); ';
  172. }
  173. }
  174. }
  175. ?>