分页类

<?php

class Page_z
{
    /**
     * 上一页显示的内容
     */
    public $prev = '<';

    /**
     * 下一页显示的内容
     */
    public $next = '>';

    /**
     * 第一页显示的内容
     */
    public $first = '|<<';

    /**
     * 导航条显示的内容
     */
    public $lineNav = '%s';

    /**
     * 导航条显示几个
     */
    public $lineNavCount = 10;

    /**
     * 最后一页显示的内容
     */
    public $last = '>>|';

    /**
     * 页的变量名
     */
    public $pageVar = 'page_z';

    /**
     * 记录偏移地址, 计算值
     */
    public $offset;

    /**
     * 总页数
     */
    public $totalPage;

    /**
     * 总条数
     */
    public $total;
    /**
     * 页大小, 每页多少条
     */
    public $pageSize;

    /**
     * 当前页码
     */
    public $curPageNum;

    /**
     * 上一页页码
     */
    public $prevPageNum;

    /**
     * 下一页页码
     */
    public $nextPageNum;

    /**
     * 首页页码, 如果当前是第一页, 则为false
     */
    public $firstPageNum;

    /**
     * 尾页页码, 如果当前是最后一页, 则为false
     */
    public $lastPageNum;

    /**
     * 基础地址
     */
    public $path;

    /**
     * 分解成数组的query_string
     */
    public $queryStringArray;

    public function __construct( $total, $pageSize )
    {
        $this -> total = $total;
        $this -> pageSize = $pageSize;
        $this -> totalPage = ceil( $total / $pageSize );
        $curPageNum = !empty( $_REQUEST[ $this -> pageVar ] )
            ? $_REQUEST[ $this -> pageVar ]
            : 1;
        $this -> curPageNum = filter_var( $curPageNum,
            FILTER_VALIDATE_INT,
            array(
                'options' => array(
                    'min_range' => 1,
                    'max_range' => $this -> totalPage,
                    'default' => 1,
                ),
            )
        );
        $this -> prevPageNum = filter_var( $this -> curPageNum - 1,
            FILTER_VALIDATE_INT,
            array(
                'options' => array(
                    'min_range' => 1,
                ),
            )
        );
        $this -> nextPageNum = filter_var( $this -> curPageNum + 1,
            FILTER_VALIDATE_INT,
            array(
                'options' => array(
                    'max_range' => $this -> totalPage,
                ),
            )
        );
        $this -> firstPageNum = ( $this -> curPageNum == 1 ) ? false : 1;
        $this -> lastPageNum = ( $this -> curPageNum == $this -> totalPage ) ? false : $this -> totalPage;
        $this -> offset = ( $this -> curPageNum - 1 ) * $this -> pageSize;
        $this -> assignUrl( $_SERVER[ 'REQUEST_URI' ] );

    }

    /**
     * 给URL相关变量赋值
     * 目前只跳转到本站
     */
    public function assignUrl( $url )
    {
        $urlInfo = parse_url( $url );
        $this -> path = $urlInfo[ 'path' ];
        // 使用默认值数组合并
        $urlInfo = array_merge( array( 'path' => '', 'query' => '' ), $urlInfo );
        parse_str( $urlInfo[ 'query' ], $this -> queryStringArray );
    }


    /**
     * 取偏移
     */
    public function getOffset()
    {
        return $this -> offset;
    }

    /**
     * 生成某页的href
     */
    public function getHref( $pageNum )
    {
        $pageNum = filter_var( $pageNum, FILTER_VALIDATE_INT,
            array(
                'options' => array(
                    'min_range' => 1,
                    'max_range' => $this -> totalPage,
                ),
            )
        );
        if( $pageNum )
        {
            $qsArr = $this -> queryStringArray;
            $qsArr[ $this -> pageVar ] = $pageNum;
            $href = $this -> path . '?' . http_build_query( $qsArr );
        } else
        {
            $href = 'javascript:void(0);';
        }
        return $href;
    }

    /**
     * 首页的连接: 类似 <a href='XXX' class='XXX'>第一页</a>
     */
    public function getFirstLink( $class = 'first_z' )
    {
        $link = sprintf( '<a href="%s" class="%s">%s</a>',
            $this -> getHref( $this -> firstPageNum ),
            $class,
            $this -> first
        );
        return $link;
    }

    /**
     * 尾页连接
     */
    public function getLastLink( $class = 'last_z' )
    {
        $link = sprintf( '<a href="%s" class="%s">%s</a>',
            $this -> getHref( $this -> lastPageNum ),
            $class,
            $this -> last
        );
        return $link;
    }

    /**
     * 上一页
     */
    public function getPrevLink( $class = 'prev_z' )
    {
        $link = sprintf( '<a href="%s" class="%s">%s</a>',
            $this -> getHref( $this -> prevPageNum ),
            $class,
            $this -> prev
        );
        return $link;
    }

    /**
     * 下一页
     */
    public function getNextLink( $class = 'next_z' )
    {
        $link = sprintf( '<a href="%s" class="%s">%s</a>',
            $this -> getHref( $this -> nextPageNum ),
            $class,
            $this -> next
        );
        return $link;
    }

    /**
     * 中间的数字导航 1..10
     */
    public function getLineNav( $class = 'line_z', $curClass = 'cur' )
    {
        // 第一个, 取 (当前页 - 条数/2) 和 (总页数 - 条数) 的小数
        $lineFirst = filter_var(
            min( $this -> curPageNum - floor( ( $this -> lineNavCount - 1 ) / 2 ),
                $this -> totalPage - $this -> lineNavCount + 1 ),
            FILTER_VALIDATE_INT,
            array( 'options' =>
                array(
                    'min_range' => 1,
                    'default' => 1,
                ),
            )
        );
        $lineLast = filter_var( $lineFirst + $this -> lineNavCount - 1,
            FILTER_VALIDATE_INT,
            array( 'options' =>
                array(
                    'max_range' => $this -> totalPage,
                    'default' => $this -> totalPage,
                ),
            )
        );
        $links = array();
        for( $i = $lineFirst; $i <= $lineLast; $i ++ )
        {
            if( $i == $this -> curPageNum )
            {
                // 当前页的连接处理
                $href = $this -> getHref( false );
                $links[] = sprintf( '<a href="%s" class="%s">' . $this -> lineNav . '</a>',
                    $href,
                    $curClass,
                    $i );

            } else
            {
                $href = $this -> getHref( $i );
                $links[] = sprintf( '<a href="%s" class="%s">' . $this -> lineNav . '</a>',
                    $href,
                    $class,
                    $i );
            }
        }

        return implode( ' ', $links );

    }

    /**
     * 简单显示
     */
    public function show()
    {
        $links[ 'first' ] = $this -> getFirstLink();
        $links[ 'prev' ] = $this -> getPrevLink();
        $links[ 'line' ] = $this -> getLineNav();
        $links[ 'next' ] = $this -> getNextLink();
        $links[ 'last' ] = $this -> getLastLink();
        echo implode( ' ', $links );
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值