yii2.0 分页(二) [ 2.0 版本 ]
下面小伙为大家介绍第二种分页方法:
控制器 CommentController 里面的任意一个方法,在这里我的方法是 actionComment();
<?php
use yii\data\Pagination;
use app\models\Comment;
public function actionComment(){
$data = Comment::find()->andWhere(['id' => '10']);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '2']);
$model = $data->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('comment',[
'model' => $model,
'pages' => $pages,
]);
}
?>
好的,到这里,控制器部分基本就结束了。我们接续看 view 里面的代码:
Comment.php 文件代码如下所示
<?php
use yii\widgets\LinkPager;
?>
foreach($model as $key=>$val)
{
这里就是遍历数据了,省略......
}
<?= LinkPager::widget(['pagination' => $pages]); ?>
小伙儿 陕西省
注册时间:2014-08-18
最后登录:2020-06-03
在线时长:295小时3分
最后登录:2020-06-03
在线时长:295小时3分
- 粉丝272
- 金钱2077
- 威望175
- 积分6777
共 15 条评论
顶小伙儿!
舰长你真好,爱死你了
顶一下,好简单的样子~
真不错 我运行起来了 谢谢
嘿嘿嘿,好好干好好干
有没有使用DAO的?
dao就写SQL了不是?
为什么我的一运行就是这个错误
syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ']'
语法错误啦,
上面所说的那种分页形式已经搞定了,只是现在又遇到了新的问题。
如果我是使用了表单,获得表单中用户提交的数据,然后进行模糊查询,这个怎么完成分页呢?求大神指教。
这么久了,您应该早就参透顺利成为大神了吧~小弟跪下求教啊
成天工作 都没什么时间学习 鸡毛
@小伙儿 111
<?php
namespace app\backend\modules\applyadmin\controllers;
use Yii;
use yii\web\Controller;
use app\frontend\models\Apply;
use yii\data\Pagination;
class DefaultController extends Controller
{
function actionIndex(){ $query = Apply::find(); $countQuery = clone $query; $pages = new Pagination(['totalCount' =>$countQuery->count(),'pageSize' => '2']); $models = $query->offset($pages->offset) ->limit($pages->limit) ->OrderBy(['status'=>SORT_DESC]) ->all(); $this->render('index', [ 'models'=>$models, 'pages'=>$pages, ]); }
}
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use \yii\helpers\Markdown;
use \yii\helpers\Url;
?>
<?php foreach ($models as $val) :?>
<?php echo $val->id;?>
<?php endforeach; ?>
<?php
// 显示分页
echo LinkPager::widget([
'pagination' => $pages,
]);
?>
为什么我的这样输出不了数据 在foreach 外面就能输出数据。
数据格式是
array(2) { [0]=> object(app\frontend\models\Apply)#35 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(5) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(2) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(5) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(2) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(2) { ["beforeInsert"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } ["beforeUpdate"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } } ["_behaviors":"yii\base\Component":private]=> array(1) { [0]=> object(yii\behaviors\TimestampBehavior)#39 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } } } [1]=> object(app\frontend\models\Apply)#48 (8) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(2) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(1) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(7) { ["id"]=> int(2) ["uid"]=> string(1) "1" ["centent"]=> string(516) "a:20:{s:9:"is_people";s:1:"1";s:7:"company";s:11:"dsadsaddsad";s:7:"license";s:6:"dsadas";s:5:"legal";s:6:"dsadsa";s:6:"number";s:6:"dsadsa";s:8:"business";s:6:"dsadsa";s:7:"address";s:4:"dsad";s:5:"range";s:8:"dsadsdsa";s:4:"tell";s:6:"dsadsa";s:4:"name";s:6:"dsadsa";s:6:"iphone";s:9:"dsadsaads";s:11:"license_img";s:0:"";s:8:"code_img";s:0:"";s:9:"legal_img";s:0:"";s:5:"apply";s:0:"";s:10:"safety_img";s:0:"";s:12:"sanitary_img";s:0:"";s:8:"star_img";s:0:"";s:9:"plate_img";s:0:"";s:5:"agree";a:1:{i:0;s:1:"1";}}" ["is_people"]=> int(0) ["status"]=> int(1) ["created_at"]=> int(0) ["updated_at"]=> int(0) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(2) { ["beforeInsert"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } ["beforeUpdate"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } } ["_behaviors":"yii\base\Component":private]=> array(1) { [0]=> object(yii\behaviors\TimestampBehavior)#49 (5) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["owner"]=> RECURSION } } } }
是不是表有问题还是咋呢 一直纠结
额, 可读性太差了
不能截图啊。抱歉。主要是views 层有值缺不能遍历出来。我把分页方法封装好了就可以了
因为这是一个对象,不是真正意义上的数组
为什么我的代码会报错
Call to a member function count() on a non-object
$pages = new Pagination(['totalCount'=>$data->count(),'pageSize'=>'2']);
你用gettype()取得$data变量的类型,返回object是正确的,估计你把$data已经$data->all()了,->all() 返回的是array
确实是,但我返回的必须是全部的数据啊
好了 ,多谢指点
<?php
echo 'asdasd';
?>
顶一下,继续加油
怎么实现无刷新啊
啦啦啦啦啦啦啦
为何这么开心?
@小伙儿 因为他终于找到了他的滑板鞋!
我想问一下 数组的数据格式怎么分
请教一下大哥
没太明白你的意思 你的意思是 你查出来一个 大数组 然后开做分页 是吗
就像这样 $query = Apply::find()->all(); 得到的结果$query是个数组 那就不能调用$query->offset()方法 这样的话怎么进行分页 我把这个数组转成对象也不行
请问这个问题解决了吗?想向您请教一下,谢谢啦,也遇到了相同的问题
@38024 不要写-all()方法,你看清楚,上面就写到andWhere(),再分别count()和all()
<?php foreach($model as $key=>$val): ?>
<?php echo $val->id;?> <?php echo $val->ch_name;?> <?php echo '<br>';?>
<?php endforeach ?>
这样就可以输出了,前提 是有id,ch_name 等名称,你可以先把$val 打印出来看下结构<?var_dump($val);?>
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '5', 'params'=>['category'=>$c,'dicttype'=>$d]]);
为什么我加了个参数params,就无法切换分页了呢
'params'=>array_merge($_GET, ['category'=>$c,'dicttype'=>$d]) 这样加