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頁面

相關文章
相關標籤/搜索