YII分页显示数据

1.分页控制器:php

为懒人附上代码:html

namespace app\controllers;
use yii\web\Controller;web

use app\models\Users;//对应的数据表模型
use yii\data\Pagination; //分页类数据库


Class HelloController extends Controller{app


    //数据库分页
    public function actionList(){
        
        $request = \YII::$app->request;yii

        if(empty($page)){
            $page = $request ->get('page') ? $request ->get('page') : 1;
        }this

        $pageSize = 3;spa

        //查询语句
        $query = Users::find() ->where(['>','id',0]);htm

        //获取记录条数
        $count = $query ->count();
        $pages = new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);get

        //$users = $query ->offset($page*$pageSize) ->limit($pageSize) ->asArray() ->all();
        $users = $query ->offset(($page-1)*$pageSize) ->limit($pageSize) ->all();

        return $this ->renderPartial('list.php',['users'=>$users,'pages'=>$pages]);
    }

 

}

 

注意:当前页参数yii默认用的是page。

 

2. html页面

相关文章
相关标签/搜索