YII2.0框架分页
这篇文章主要介绍了Yii分页用法,以实例形式详细分析了比较常见的几种分页方法及其应用特点,非常具有实用价值,需要的朋友可以参考下:
在这里我主要联查的 book 表和 book_press 两张表进行分页的
Controller
/** *@action yii多表链接查询后分页 *@----------------------------------------------------------- *@ book 表和 bookpress 联查后分页 */ public function actionIndex() { $model = new Book; //实例化model $data = $model -> pages(); //调用pages 进行联查数据 $pages = new Pagination([ "totalCount" =>$data->count(), "pageSize" =>5, //pageSize 每页显示的条数 ]); $models = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all(); return $this->render("index",[ "models" => $models, "pages" => $pages, ]); }
Model
/** * @inheritdoc 多表联查数据 */ function pages() { //注意: 查询的时候不能加 all 或者 asArray 查出来是对象就可以了 return $this->find() ->select("*") ->innerJoin("`book_press` as bp on `bp`.`book_id` = `book`.`title_id`"); /* 此处去掉 ->asArray() ->all(); */ }
VIEWS
<?php use yiihelpersHtml; use yiiwidgetsLinkPager; ?> <table border=1> <?php foreach ($models as $model): ?> <tr> <td><?= $model["title"]; ?></td> <td><?= $model["author"]; ?></td> </tr> <?php endforeach; ?> </table> <?= LinkPager::widget(["pagination" => $pages, "firstPageLabel" => "首页", "lastPageLabel" => "最后一页", "prevPageLabel" => "上一页", "nextPageLabel" => "下一页", "maxButtonCount"=>5, //控制每页显示的页数 ]); ?>
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
- 下一篇: PHP知识大全