首页 > PHP > TP3.2分页查询

TP3.2分页查询

时间:2023-04-20浏览次数:242

1、

$User  = M('User');                                 // 实例化User对象
$count = $User->where('status=1')->count();         // 查询满足要求的总记录数
$Page  = new \Think\Page($count,25);                // 实例化分页类 传入总记录数和每页显示的记录数(25)
$show  = $Page->show();                            // 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list  = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);                       // 赋值数据集
$this->assign('page',$show);                       // 赋值分页输出
$this->display();                                  // 输出模板


2、

public function index($sid = 0, $p = 1)
{
    $p = intval($p) > 0 ? $p : 1;

    $article = M('article');
    $pagesize = 20;                #每页数量
    $offset = $pagesize * ($p - 1);//计算记录偏移量
    $prefix = C('DB_PREFIX');
    $sid = isset($_GET['sid']) ? $_GET['sid'] : '';
    $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
    $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
    $where = '1 = 1 ';
    if ($sid) {
        $where .= " and {$prefix}article.sid = ".$sid;
    }
    if ($keyword) {
        $where .= " and {$prefix}article.title like '%{$keyword}%' ";
    }

    $count = $article->where($where)->count();
    $list = $article->field("{$prefix}article.*,{$prefix}artical_type.type_name")->where($where)->order("{$prefix}article.save_time desc")->join("{$prefix}artical_type ON {$prefix}artical_type.id = {$prefix}article.sid")->limit($offset . ',' . $pagesize)->select();

    $page = new \Think\Page($count, $pagesize);
    $page = $page->show();
    $this->assign('list', $list);
    $this->assign('page', $page);
    $this->display();
}


3、关联查询

$prefix = C('DB_PREFIX');
$typelist = M('article')
    ->alias('a')
    ->field('a.*,b.type_name')
    ->join('left join '.$prefix.'artical_type b ON a.type_id = b.id')
    ->where("....")->select();