1.分页控制器:
为懒人附上代码:
namespace app\controllers;
use yii\web\Controller;use app\models\Users;//对应的数据表模型
use yii\data\Pagination; //分页类 Class HelloController extends Controller{ //数据库分页 public function actionList(){ $request = \YII::$app->request;if(empty($page)){
$page = $request ->get('page') ? $request ->get('page') : 1; }$pageSize = 3;
//查询语句
$query = Users::find() ->where(['>','id',0]);//获取记录条数
$count = $query ->count(); $pages = new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);//$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页面