php分页程序paginator程序代码
- <?php
-
- class paginator {
-
- function __construct () {
-
- }
-
- static function render($page_type, array $args = array()) {
- $html = '';
- switch ($page_type)
- {
- case '1':
- $html = page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v);
- break;
- case '2':
- $html = fpage($row_num, $pagesize, $page, $url);
- break;
- case '3':
- break;
-
- default:
- $html = page($num, $perpage, $curpage, $mpurl, $hiddenFrom='');
- }
-
- return $html;
- }
-
-
-
-
- function page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v) {
-
- $base_url .= strpos($base_url, '?') ? '&' : '?';
-
- $page_val = "";
-
- if ($page_num < 1) {
- return $page_val;
- } elseif ($page_num == 1) {
- return $page_val = 1;
- }
-
-
-
- $group_ok = !(($page_num-1) % ($per_group-1));
-
- if($group_ok) {
- $group_num = ($page_num-1)/($per_group-1);
- }else {
- $group_num = ceil(($page_num-1) / ($per_group-1));
- }
-
-
-
- $is_last_page = !(($cur_page-1) % ($per_group-1));
- if ($is_last_page) {
- $cur_group = ($cur_page-1) / ($per_group-1) + 1;
- } else {
- $cur_group = ceil(($cur_page-1) / ($per_group-1));
- }
-
-
- $from = ($cur_group - 1) * ($per_group-1) + 1;
- $from = $cur_group <= 1 ? 1 :$from;
-
- $to = $from + $per_group;
- $to = $to > $page_num ? $page_num + 1 : $to;
-
- for($a = $from; $a < $to; $a++) {
- if ($a != $cur_page) {
-
-
- $page_val .= '<li><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $a . '">' . $a . '</a></li>';
- } else {
-
- $page_val .='<li class="current">'. $a .'</li>';
- }
- }
-
-
- $next = $cur_page + 1;
- $prev = $cur_page-1;
-
- if ($cur_page != $page_num) {
-
- $page_val .= '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $next . '">' . '下一页' . '</a></span>';
- $page_val .= '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $page_num . '">' . '>>' . '</a></span>';
-
- }
- if ($cur_page != 1){
-
-
- $page_val = '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=' . $prev . '">' . '上一页' . '</a></span>' . $page_val;
- $page_val = '<span><a href ="' . $base_url . 'channel='.$channel_code.'&n='.$n.'&v='.$v.'&page=1">' . '<<' . '</a></span>' . $page_val;
-
- }
- $page_val = '<ul class="page_nav">'.$page_val.'</ul>';
-
-
- return $page_val;
- }
-
-
-
-
-
- function fpage($row_num, $pagesize, $page, $url) {
-
-
- $page_count = $row_num % $pagesize?intval($row_num / $pagesize) + 1:intval($row_num / $pagesize);
-
- if ($page_count == 1) {
- $page_string = "第一页|上一页|下一页|尾页";
- } else {
-
- if ($page == 1) {
-
- $page_string = " <a href=" . $url . "?page=1>第一页 </a>|上一页| <a href=" . $url . "?page=" . ($page + 1) . ">下一页 </a>| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
- }
- if (($page > 1) and ($page < $page_count)) {
- $page_string = " <a href=" . $url . "?page=1>第一页 </a>| <a href=" . $url . "?page=" . ($page-1) . ">上一页 </a>| <a href=" . $url . "?page=" .
- ($page + 1) . ">下一页 </a>| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
- }
- if ($page == $page_count) {
- $page_string = " <a href=" . $url . "?page=1>第一页 </a>| <a href=" . $url . "?page=" . ($page-1) . ">上一页 </a>|下一页| <a href=" . $url . "?page=" . $page_count . ">尾页 </a>";
- }
- }
- $page_str = $page_string . ("," . $page . "/" . $page_count . "页");
- echo " <br> <p align=center>" . $page_str . " </p>";
- }
-
-
-
-
-
-
-
-
-
-
-
-
- function page($num, $perpage, $curpage, $mpurl,$hiddenFrom='') {
- $multipage = '';
-
- $mpurl .= strpos($mpurl, '?') ? '&' : '?';
-
- if($num > $perpage) {
- $page = 10;
- $offset = 2;
-
- $pages = @ceil($num / $perpage);
-
- if($page > $pages) {
- $from = 1;
- $to = $pages;
- } else {
-
- $from = $curpage - $offset;
- $to = $from + $page - 1;
-
- if($from < 1) {
-
- $to = $curpage + 1 - $from;
- $from = 1;
- if($to - $from < $page) {
- $to = $page;
- }
- } elseif($to > $pages) {
- $from = $pages - $page + 1;
- $to = $pages;
- }
- }
-
- if ($hiddenFrom) {
- $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="#" '.js_page($hiddenFrom,1).'>首页</a> ' : '').
- ($curpage > 1 ? ' <a href="#" '.js_page($hiddenFrom,$curpage - 1).'>‹‹上一页</a> ' : '');
- for($i = $from; $i <= $to; $i++) {
- $multipage .= $i == $curpage ? '<span>'.$i.'</span> ' :
- '<a href="#" '.js_page($hiddenFrom,$i).'>'.$i.'</a> ';
- }
-
- $multipage .= ($curpage < $pages ? '<a href="#" '.js_page($hiddenFrom,$curpage + 1).'>下一页››</a>' : '').
- ($to < $pages ? '<a href="#" '.js_page($hiddenFrom,$pages).'> 末页</a>' : '');
- $multipage = $multipage ? '<div class="list-page">'.$multipage.'</div>' : '';
- } else {
-
- $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1">首页</a> ' : '').
- ($curpage > 1 ? ' <a href="'.$mpurl.'page='.($curpage - 1).'">‹‹上一页</a> ' : '');
-
- for($i = $from; $i <= $to; $i++) {
- $multipage .= $i == $curpage ? '<span>'.$i.'</span> ' :
- '<a href="'.$mpurl.'page='.$i.'">'.$i.'</a> ';
- }
-
- $multipage .= ($curpage < $pages ? '<a href="'.$mpurl.'page='.($curpage + 1).'">下一页››</a>' : '').
- ($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'"> 末页</a>' : '');
- $multipage = $multipage ? '<div class="list-page">'.$multipage.'</div>' : '';
- }
- }
- return $multipage;
- }
-
- function page_get_start($page, $ppp, $totalnum) {
- $totalpage = ceil($totalnum / $ppp);
- $page = max(1, min($totalpage, intval($page)));
- return ($page - 1) * $ppp;
- }
-
- function js_page($hiddenFrom,$page)
- {
- if ($hiddenFrom) {
- return ' onclick=javascript:$("#page").val("'.$page.'");$("#'.$hiddenFrom.'").submit(); ';
- }
- }
- }
- ?>
|