thinkphp功能类之 Page.class.php

1. 构造函数 要求传入  $totalRows(总记录数) 和  $listRow(每页显示的记录数)

    /**
     * 架构函数
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     */
    public function __construct($totalRows, $listRows=20, $parameter = array()) {
        C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
        /* 基础设置 */
        $this->totalRows  = $totalRows; //设置总记录数
        $this->listRows   = $listRows;  //设置每页显示行数
        $this->parameter  = empty($parameter) ? $_GET : $parameter;
        $this->nowPage    = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
        $this->nowPage    = $this->nowPage>0 ? $this->nowPage : 1;
        $this->firstRow   = $this->listRows * ($this->nowPage - 1);
    }

2. 创建对象后 一般会根据需求更改其属性

    public $firstRow; // 起始行数
    public $listRows; // 列表每页显示行数
    public $parameter; // 分页跳转时要带的参数
    public $totalRows; // 总行数
    public $totalPages; // 分页总页面数
    public $rollPage   = 11;// 分页栏每页显示的页数
    public $lastSuffix = true; // 最后一页是否显示总页数
  //例如  
//$page->lastSuffix=false;
//$page->rollPage=4;

3 通过setConfig()来设置 一些具体的显示如

    // 分页显示定制
    private $config  = array(
        'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
        'prev'   => '<<',
        'next'   => '>>',
        'first'  => '1...',
        'last'   => '...%TOTAL_PAGE%',
        'theme'  => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
    );
        //可以设置的样式如上----------------------------------------------------
$page->setConfig('prev','上一页'); $page->setConfig('next','下一页'); $page->setConfig('last','末页'); $page->setConfig('first','首页');
 
  
 
 

4 使用limit 进行分页 查询  参数为Page类中的参数  

  $list=$model->order('id asc')
->field('sp_user.*,sp_dept.name as deptname')
->join('sp_dept on sp_user.dept_id=sp_dept.id','LEFT')
->limit($page->firstRow.','.$page->listRows)->select();

  

完整代码 如下

        $countRows=$model->count();//查询总的记录数
        $page       = new \Think\Page($countRows,3);
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','末页');
        $page->setConfig('first','首页');
        $page->lastSuffix=false;
        $page->rollPage=4;
        $show       = $page->show();// 分页显示输出
        $list=$model->order('id asc')->field('sp_user.*,sp_dept.name as deptname')
->join('sp_dept on sp_user.dept_id=sp_dept.id','LEFT')->limit($page->firstRow.','.$page->listRows)->select(); $this->assign('list',$list); $this->assign('show',$show); $this->display();

猜你喜欢

转载自www.cnblogs.com/mofei12138/p/11571343.html
今日推荐