PHP 的一种分页函数

控制器代码

        //获取管理员列表并分页
        $page = (int)($_GET['page'])? (int)($_GET['page']):'1';
        $limit = 1;
        $total = $this->User->getAdminTotal();

        if($total>0)
        {
            $url = "/admin.php?c=index&a=index";
            $this->_data['adminList'] = $this->User->getUserList($page,$limit);
            $this->_data['pagebar'] = $this->pagebarht ( $url, array(), $limit, $page, $total );
        }

方法

 /**
     * 分页方法
     * @param $url
     * @param $param
     * @param $limit
     * @param $page
     * @param $total
     * @return bool|string
     */
    function Pagebarht($url, $param, $limit, $page, $total) {
        $str_html = '';
        if ($total < 0) {
            return false;
        }
        if ($url == "") {
            return false;
        }
        $link = $url . "&";
        if (is_array ( $param )) {
            foreach ( $param as $str_key => $str_value ) {
                $link = $link . "$str_key=" . urlencode ( $str_value ) . "&";
            }
        }
        $int_pages = ceil ( $total / $limit );
        if ($page < 1) {
            $page = 1;
        }
        if ($page > $int_pages) {
            $page = $int_pages;
        }
        $start_url = $link . "page=1";
        $end_url = $link . "page=$int_pages";
        $pre_url = $link . "page=" . ($page - 1);
        $next_url = $link . "page=" . ($page + 1);
        if ($page < 6) {
            $start_page = 1;
            $end_page = 7;
        } else {
            $start_page = $page - 5;
            $end_page = $page + 1;
        }
        if ($end_page > $int_pages) {
            $end_page = $int_pages;
        }
        $urls = null;
        /**
         * THE URL
         */
        for($i = $start_page, $j = 0; $i <= $end_page; $i ++, $j ++) {
            $temp_url = $link . "page=$i";
            if ($i == $page) {
                $urls [$j] = "<font class='psel'>" . $i . "</font>";
            } else {
                $urls [$j] = "<a href=\"$temp_url\">" . $i . "</a>";
            }
        }
        if (is_array ( $urls )) {
            $str_html = "<script>function doSubmit(value){if(isNaN(value)){value = 1;} var totPage=$int_pages;if(value>totPage){value = totPage;}if(value<1){value = 1;}window.location.href='$url&page='+value;}</script>";
            $str_html .= "<div id='pagerf' class='pager' ><span>";
            $str_html .= "<em>共" . $total . "条信息&nbsp;&nbsp;</em>";
            $str_html .= "&nbsp;<a class='pager' href=\"$start_url\" text=\回第1页\ >首页</a>&nbsp;&nbsp;";
            if ($page > 1) {
                $str_html .= "<a href=\"$pre_url\" class='pager'>上页</a>&nbsp;&nbsp;";
            } else {
                $str_html .= "<font class='page_up'>上页</font>&nbsp;&nbsp;";
            }
            foreach ( $urls as $sub_url ) {
                $str_html .= $sub_url;
            }
            if ($page >= $int_pages) {
                $str_html .= "<font class='page_down blue'>下页</font>&nbsp;&nbsp;";
            } else {
                $str_html .= "<a href=\"$next_url\" class='page_down blue'>下页</a>&nbsp;&nbsp";
            }
            $str_html .= "<a href=\"$end_url\" text=\"到第$int_pages\">尾页</a> &nbsp;&nbsp;";
            $str_html .= "<em>共" . $int_pages . "页 &nbsp;</em>";
            $str_html .= "<em>跳到 <input type=text class=ht_inpp size=4 onBlur='doSubmit(this.value)' /> 页 </em>";
            $str_html .= "<a href='javascript:void(0)'>确定</a>";
            $str_html .= "</span></div>";
            return $str_html;
        }
        return false;
    }

展示: