view视图部分
<?phpuse yii\widgets\LinkPager;
foreach($models as $k=>$v){
echo "<tr>";
echo "<td>".$v['u_id']."</td>";
echo "<td>".$v['u_name']."</td>";
echo "<td>".$v['u_state']."</td>";
echo "</tr>";
}
echo LinkPager::widget([
'pagination' => $pages,
]);
?>
控制器部分页
<?php
use yii\data\Pagination;
public function actionList()
{
$test=new TestForm(); //实例化model模型
$arr=$test->find();
//$countQuery = clone $arr;
$pages = new Pagination([
//'totalCount' => $countQuery->count(),
'totalCount' => $arr->count(),
'pageSize' => 2 //每页显示条数
]);
$models = $arr->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('list', [
'models' => $models,
'pages' => $pages
]);
}
?>
控制器两表联查+分页 yii控制器中的方法
public function actionHuihualist(){
$cla_id=yii::$app->request->get('cla_id');
// $cla_id=3;//测试数据
$test=new Huihua(); //实例化model模型
$arr=$test->find();
//$countQuery = clone $arr;
$pages = new Pagination([
//'totalCount' => $countQuery->count(),
'totalCount' => $arr->count(),
'pageSize' => 2 //每页显示条数
]);
$models = $arr->select('*') //查询的对象指向
->innerJoin('student','huihua.h_stu_id=student.stu_id') //两表联查
->where(['h_cla_id'=>$cla_id]) //查询的条件
->offset($pages->offset) //每页的偏移量
->limit($pages->limit) //每页的条数
->asArray() //转化成数组
->all();
return $this->render('huihualist', [
'data' => $models, //把页面要循环的值传过去,data是名字,$models是查出来的值
'pages' => $pages,
// 'data'=>$data
]);
}
//未分页的两表联查
// $h=new Huihua();
// $data=$h->find()->select('*')->innerJoin('student','huihua.h_stu_id=student.stu_id')->where(['h_cla_id'=>$cla_id])->asArray()->all();
// return $this->render('huihualist',['data'=>$data]);