程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php分頁程序paginator程序代碼

php分頁程序paginator程序代碼

編輯:關於PHP編程

本文章來給大家介紹一個paginator的php分頁程序代碼,有需要的朋友可進入參考。  代碼如下 復制代碼 <?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;
    }
 
 
 
    //函數1:根據總頁數,當前頁,和頁分組及url產生分頁導航
    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; //如果小於1,返回空
        } elseif ($page_num == 1) {
            return $page_val = 1;   //如果等於1,返回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;    //當前頁+1
        $prev = $cur_page-1;   //當前頁-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;
    }
 
 
 
 
    // 函數2:根據記錄數,頁列清數,當前頁,url年成分頁
    function fpage($row_num, $pagesize, $page, $url) {
        //$page,當前頁;$row_num記錄總數;$pagesize:每頁記錄數;$url記錄頁;
        //如果,如果有作數,則取加1
        $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>";
    }
 
 
    /**
 * 分頁函數
 *
 * @param int $num:記錄總數
 * @param int $perpage:每頁記錄
 * @param int $curpage:當前頁
 * @param string $mpurl:路徑url
 * @param unknown_type $hiddenFrom
 * @return string
 */
    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 {
                //如果總頁數>10
                $from = $curpage - $offset;   //當前頁-2
                $to = $from + $page - 1;    //當前頁+10-3
 
                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(); ';
        }
    }
}
 
 
 
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved