分页处理

<?php

class Page{

    private $sysInfo;
    /*
    * 存放程序关联信息
    *
    * @var array
    * @access private
    */
   
    private $data;
    /*
    * 存放数据库数据
    *
    * @var array
    * @access private
    */
   
    private $sql;
    /*
    * 数据库对象实例化
    *
    * @var DBPDO
    * @access private
    */
   
    public function __construct( $key , $table , $where="" , $records=10 , $maxPage=10 )
    {
        /*
        * 创建对象
        *
        * @param string $key
        * @param string $table
        * @param string $where
        * @param int $records
        * @param int $maxPage
        * @access public
        */
       
        settype( $_GET[ $key ] , "integer" );
        $this->sql                         = new DBPDO(  );
        $this->sysInfo['key']             = $key;
        $this->sysInfo['page']             = max( $_GET[ $key ]-1 , 0 );
        $this->sysInfo['table']            = $table;
        $this->sysInfo['where']            = $where;
        $this->sysInfo['records']        = $records;
        $this->sysInfo['maxPage']        = $maxPage;
    }
   
    private function query(  )
    {
        /*
        * 获取记录相关信息
        *
        * @access private
        */
       
        $query =  "select count(*) from ".$this->sysInfo['table'];
        $query .= $this->sysInfo['where'] ? " where ".$this->sysInfo['where'] : "";
       
        if( !$this->sql->query( $query ) )
        {
            return( false );
        }
       
        $result = $this->sql->GetResult(  );
        $this->sysInfo['resTotal'] = $result[0];
        $this->sysInfo['record'] = $this->sysInfo['page']*$this->sysInfo['records'];
        $query = "select * from ".$this->sysInfo['table'];
        $query .= $this->sysInfo['where'] ? " where ".$this->sysInfo['where'] : "";
        $query .= " limit ".$this->sysInfo['record'].",".$this->sysInfo['records'];
       
        if( !$this->sql->query( $query ) )
        {
            return( false );
        }
       
        $this->data = $this->sql->GetResults(  );
        $this->sysInfo['queryTotal']         = count( $this->data );
        $this->sysInfo['pageTotal']          = ceil( $this->sysInfo['resTotal'] / $this->sysInfo['records'] );
        $currentPage                        = $this->sysInfo['page'] % $this->sysInfo['maxPage'];
        $start = min( $this->sysInfo['pageTotal'] , floor( $this->sysInfo['page'] / $this->sysInfo['maxPage'] )*$this->sysInfo['maxPage'] );
        $this->sysInfo['currentPageStart']    = max( $start , 0 );
        $this->sysInfo['currentPageEnd']     = min( $this->sysInfo['currentPageStart']+$this->sysInfo['maxPage'] , $this->sysInfo['pageTotal'] );
        $this->sysInfo['next']                  = min( $this->sysInfo['pageTotal'] , $this->sysInfo['page']+1 );
        $prev = min( $this->sysInfo['pageTotal'] , $this->sysInfo['page'] );
        $this->sysInfo['prev']                = max( 0 , $prev-1 );
        return( true );
    }

    public function __get( $property )
    {
        /*
        * 获取对象的关联信息
        *
        * @param string $property
        * @access public
        */
       
        switch( strtolower( $property ) )
        {
            case "key":
                return( $this->sysInfo['key'] );
            break;
            case "value":
                return( $this->sysInfo['value'] );
            break;
            case "table":
                return( $this->sysInfo['table'] );
            break;
            case "where":
                return( $this->sysInfo['where'] );
            break;
            case "recordtotal":
                return( $this->sysInfo['recordTotal'] );
            break;
            case "maxpage":
                return( $this->sysInfo['maxPage'] );
            break;
            case "restotal":
                return( $this->sysInfo['resTotal'] );
            break;
            case "pagetotal":
                return( $this->sysInfo['pageTotal'] );
            break;
            case "currenttotal":
                return( $this->sysInfo['currentPageTotal'] );
            break;
            case "start":
                return( $this->sysInfo['currentPageStart'] );
            break;
            case "end":
                return( $this->sysInfo['currentPageEnd'] );
            break;
            case "next":
                return( $this->sysInfo['next'] );
            break;
            case "prev":
                return( $this->sysInfo['prev'] );
            break;
            case "sysinfo":
                return( $this->sysInfo );
            break;
            default:
                return( false );
        }
    }
   
    public function getRecords(  )
    {
        /*
        * 获取数据库记录信息
        *
        * @access public
        */
       
        if( !$this->data )
        {
            if( !self::query(  ) )
            {
                return( false );
            }
        }
       
        return( $this->data );
    }
   
    public function showError(  )
    {
        return( $this->error->pross( ) );
    }
   
    public function __destruct(  )
    {
        /*
        * 释放对象占用的资源
        *
        * @access public
        */
       
        $this->sql       = NULL;
        $this->sysInfo = NULL;
        $this->data    = NULL;
    }

}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值