首頁 >php教程 >php手册 >php分页程序paginator程序代码

php分页程序paginator程序代码

WBOY
WBOY原創
2016-05-25 16:54:19973瀏覽

本文章来给大家介绍一个paginator的php分页程序代码,有需要的朋友可进入参考。

<?php
class paginator {
    function __construct() {
    }
    static function render($page_type, array $args = array()) {
        $html = &#39;&#39;;
        switch ($page_type) {
            case &#39;1&#39;:
                $html = page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v);
                break;
            case &#39;2&#39;:
                $html = fpage($row_num, $pagesize, $page, $url);
                break;
            case &#39;3&#39;:
                break;
            default:
                $html = page($num, $perpage, $curpage, $mpurl, $hiddenFrom = &#39;&#39;);
        }
        return $html;
    }
    //函数1:根据总页数,当前页,和页分组及url产生分页导航
    function page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v) {
        // 分页函数参数列表(有多少页,当前页,每页多少个 ,链接地址)
        $base_url.= strpos($base_url, &#39;?&#39;) ? &#39;&&#39; : &#39;?&#39;; //如果有?,则添加&,否则加?
        $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.= &#39;<li><a href ="&#39; . $base_url . &#39;channel=&#39; . $channel_code . &#39;&n=&#39; . $n . &#39;&v=&#39; . $v . &#39;&page=&#39; . $a . &#39;">&#39; . $a . &#39;</a></li>&#39;;
            } else {
                //当前页
                $page_val.= &#39;<li class="current">&#39; . $a . &#39;</li>&#39;;
            }
        }
        //上一页下一页首页尾页
        $next = $cur_page + 1; //当前页+1
        $prev = $cur_page - 1; //当前页-1
        if ($cur_page != $page_num) {
            //当前页不是最后一页
            $page_val.= &#39;<span><a href ="&#39; . $base_url . &#39;channel=&#39; . $channel_code . &#39;&n=&#39; . $n . &#39;&v=&#39; . $v . &#39;&page=&#39; . $next . &#39;">&#39; . &#39;下一页&#39; . &#39;</a></span>&#39;;
            $page_val.= &#39;<span><a href ="&#39; . $base_url . &#39;channel=&#39; . $channel_code . &#39;&n=&#39; . $n . &#39;&v=&#39; . $v . &#39;&page=&#39; . $page_num . &#39;">&#39; . &#39;>>&#39; . &#39;</a></span>&#39;; //尾页
            
        }
        if ($cur_page != 1) {
            //不是第一页,加上前面的内容
            $page_val = &#39;<span><a href ="&#39; . $base_url . &#39;channel=&#39; . $channel_code . &#39;&n=&#39; . $n . &#39;&v=&#39; . $v . &#39;&page=&#39; . $prev . &#39;">&#39; . &#39;上一页&#39; . &#39;</a></span>&#39; . $page_val;
            $page_val = &#39;<span><a href ="&#39; . $base_url . &#39;channel=&#39; . $channel_code . &#39;&n=&#39; . $n . &#39;&v=&#39; . $v . &#39;&page=1">&#39; . &#39;<<&#39; . &#39;</a></span>&#39; . $page_val; //首页
            
        }
        $page_val = &#39;<ul class="page_nav">&#39; . $page_val . &#39;</ul>&#39;;
        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 = &#39;&#39;) {
        $multipage = &#39;&#39;;
        //恢复
        $mpurl.= strpos($mpurl, &#39;?&#39;) ? &#39;&&#39; : &#39;?&#39;;
        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 ? &#39;<a href="#" &#39; . js_page($hiddenFrom, 1) . &#39;>首页</a> &#39; : &#39;&#39;) . ($curpage > 1 ? &#39; <a href="#" &#39; . js_page($hiddenFrom, $curpage - 1) . &#39;>&lsaquo;&lsaquo;上一页</a> &#39; : &#39;&#39;);
                for ($i = $from; $i <= $to; $i++) {
                    $multipage.= $i == $curpage ? &#39;<span>&#39; . $i . &#39;</span> &#39; : &#39;<a href="#" &#39; . js_page($hiddenFrom, $i) . &#39;>&#39; . $i . &#39;</a> &#39;;
                }
                $multipage.= ($curpage < $pages ? &#39;<a href="#" &#39; . js_page($hiddenFrom, $curpage + 1) . &#39;>下一页&rsaquo;&rsaquo;</a>&#39; : &#39;&#39;) . ($to < $pages ? &#39;<a href="#" &#39; . js_page($hiddenFrom, $pages) . &#39;> 末页</a>&#39; : &#39;&#39;);
                $multipage = $multipage ? &#39;<div class="list-page">&#39; . $multipage . &#39;</div>&#39; : &#39;&#39;;
            } else {
                $multipage = ($curpage - $offset > 1 && $pages > $page ? &#39;<a href="&#39; . $mpurl . &#39;page=1">首页</a> &#39; : &#39;&#39;) . ($curpage > 1 ? &#39; <a href="&#39; . $mpurl . &#39;page=&#39; . ($curpage - 1) . &#39;">&lsaquo;&lsaquo;上一页</a> &#39; : &#39;&#39;);
                for ($i = $from; $i <= $to; $i++) {
                    $multipage.= $i == $curpage ? &#39;<span>&#39; . $i . &#39;</span> &#39; : &#39;<a href="&#39; . $mpurl . &#39;page=&#39; . $i . &#39;">&#39; . $i . &#39;</a> &#39;;
                }
                $multipage.= ($curpage < $pages ? &#39;<a href="&#39; . $mpurl . &#39;page=&#39; . ($curpage + 1) . &#39;">下一页&rsaquo;&rsaquo;</a>&#39; : &#39;&#39;) . ($to < $pages ? &#39;<a href="&#39; . $mpurl . &#39;page=&#39; . $pages . &#39;"> 末页</a>&#39; : &#39;&#39;);
                $multipage = $multipage ? &#39;<div class="list-page">&#39; . $multipage . &#39;</div>&#39; : &#39;&#39;;
            }
        }
        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 &#39; onclick=javascript:$("#page").val("&#39; . $page . &#39;");$("#&#39; . $hiddenFrom . &#39;").submit(); &#39;;
        }
    }
}
?>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn