分页

<?php
class SubPages {
    private $each_disNums; // 每页显示的条目数
    private $nums; // 总条目数
    private $current_page; // 当前被选中的页
    private $sub_pages; // 每次显示的页数
    private $pageNums; // 总页数
    private $point; // 总页数
    private $page_array = array (); // 用来构造分页的数组
    private $subPage_link; // 每个分页的链接
    private $subPage_type; // 显示分页的类型
    /*
     * __construct是SubPages的构造函数,用来在创建类的时候自动运行.
     * @$each_disNums 每页显示的条目数
     *  @nums 总条目数
     *  @current_num 当前被选中的页
     *   @sub_pages 每次显示的页数
     *   @subPage_link 每个分页的链接
     *   @subPage_type 显示分页的类型 当
     *   @subPage_type=1的时候为普通分页模式
     *    example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 当@subPage_type=2的时候为经典分页样式 example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
    */
    function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_link, $point) {
        $this->each_disNums = intval ( $each_disNums );
        $this->nums = intval ( $nums );
        /*
         if (! $current_page) {
        $this->current_page = 1;
        } else {
        $this->current_page = intval ( $current_page );
        }
        */
        $this->current_page=$current_page+1;
        $this->sub_pages = intval ( $sub_pages );
        $this->pageNums = ceil ( $nums / $each_disNums );
        $this->subPage_link = $subPage_link;
        $this->point = $point;

        // echo $this->pageNums."--".$this->sub_pages;
    }

    function show($subPage_type=2)
    {
        return $this->show_SubPages ( $subPage_type );
    }

    /*
     * __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
    */
    function __destruct() {
        unset ( $each_disNums );
        unset ( $nums );
        unset ( $current_page );
        unset ( $sub_pages );
        unset ( $pageNums );
        unset ( $page_array );
        unset ( $subPage_link );
        unset ( $subPage_type );
    }

    /*
     * show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
    */
    function show_SubPages($subPage_type) {
        if ($subPage_type == 1) {
            return $this->subPageCss1 ();
        } elseif ($subPage_type == 2) {
            return $this->subPageCss2 ();
        }
    }

    /*
     * 用来给建立分页的数组初始化的函数。
    */
    function initArray() {
        for($i = 0; $i < $this->sub_pages; $i ++) {
            $this->page_array [$i] = $i;
        }
        return $this->page_array;
    }

    /*
     * construct_num_Page该函数使用来构造显示的条目 即使:[1][2][3][4][5][6][7][8][9][10]
    */
    function construct_num_Page() {
        if ($this->pageNums < $this->sub_pages) {
            $current_array = array ();
            for($i = 0; $i < $this->pageNums; $i ++) {
                $current_array [$i] = $i + 1;
            }
        } else {
            $current_array = $this->initArray ();
            if ($this->current_page <= 3) {
                for($i = 0; $i < count ( $current_array ); $i ++) {
                    $current_array [$i] = $i + 1;
                }
            } elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
                for($i = 0; $i < count ( $current_array ); $i ++) {
                    $current_array [$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
                }
            } else {
                for($i = 0; $i < count ( $current_array ); $i ++) {
                    $current_array [$i] = $this->current_page - 2 + $i;
                }
            }
        }

        return $current_array;
    }

    function subPageCss1() {

        $re='<ul class="pagination">';
        if ($this->current_page > 1) {
            $prewPageUrl = $this->subPage_link . ($this->current_page - 2) . $this->point;
            $re.='<li><a href="'.$prewPageUrl.'">&laquo;</a></li>';
        }else{
            $re .= '<li class="disabled"><a href="#">&laquo;</a></li>';
        }

        $a = $this->construct_num_Page ();
        for($i = 0; $i < count ( $a ); $i ++) {
            $s = $a [$i];
            if ($s == $this->current_page) {
                $re .= '<li class="active"><a href="#">' . $s . '</a></li>';
            } else {
                $url = $this->subPage_link . ($s-1) . $this->point;
                $re .= '<li><a href="'.$url.'">' . $s . '</a></li>';
            }
        }

        if ($this->current_page < $this->pageNums) {
            $nextPageUrl = $this->subPage_link . ($this->current_page) . $this->point;
            // <a href="">下一页</a><a href="">末页</a>
            $re.='<li><a href="'.$nextPageUrl.'">&raquo;</a></li>';
            //$re .= "<a href='$nextPageUrl'>&raquo;</a>";
            //$subPageCss2Str .= "<a href='$lastPageUrl'>尾页</a></font>";
        } else {
            $re .= '<li class="disabled"><a href="#">&raquo;</a>';
        }

        $re.='</ul>';

        return $re;
    }

    /*
     * 构造经典模式的分页 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
    */
    function subPageCss2() {

        $subPageCss2Str = "";
        // <span>共<b>246</b>页<b>2347</b>条商家信息</span>
        $subPageCss2Str .= "<span>当前第<b>" . $this->current_page . "</b> /<b>" . $this->pageNums . "</b> 页&nbsp;&nbsp;&nbsp;&nbsp; 共<b>" . $this->nums . "</b>条信息</span>";
        // $subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";

        if ($this->current_page > 1) {
            $firstPageUrl = $this->subPage_link . "1" . "#zkfb_shop";
            $prewPageUrl = $this->subPage_link . ($this->current_page - 1) . $this->point;
            // <a href="">首页</a><a href="">上一页</a>
            $subPageCss2Str .= "<font><a href='$firstPageUrl'>首页</a>";
            $subPageCss2Str .= "<a href='$prewPageUrl'>上一页</a>";
        } else {
            $subPageCss2Str .= "<font><a >首页</a>";
            $subPageCss2Str .= "<a >上一页</a>";
        }

        $a = $this->construct_num_Page ();
        for($i = 0; $i < count ( $a ); $i ++) {
            $s = $a [$i];
            if ($s == $this->current_page) {
                // $subPageCss2Str.="[<span style='color:red;font-weight:bold;'>".$s."</span>]";
                $subPageCss2Str .= "<a href=''class='active_zkfb_page'>" . $s . "</a>";
            } else {
                $url = $this->subPage_link . $s . $this->point;
                // <a href="">2</a>
                $subPageCss2Str .= "<a href='$url'>" . $s . "</a>";
                // $subPageCss2Str.="[<a href='$url'>".$s."</a>]";
            }
        }

        if ($this->current_page < $this->pageNums) {
            $lastPageUrl = $this->subPage_link . $this->pageNums . $this->point;
            $nextPageUrl = $this->subPage_link . ($this->current_page + 1) . $this->point;
            // <a href="">下一页</a><a href="">末页</a>
            $subPageCss2Str .= "<a href='$nextPageUrl'>下一页</a>";
            $subPageCss2Str .= "<a href='$lastPageUrl'>尾页</a></font>";
        } else {
            $subPageCss2Str .= "<a >下一页</a>";
            $subPageCss2Str .= "<a >尾页</a></font>";
        }

        return $subPageCss2Str;
    }
}

转载于:https://my.oschina.net/ailingling/blog/324795

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值