小伙儿 2014-08-21 10:42:11 61433次浏览 15条评论 35 4 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]); ?>

原文链接:http://blog.sina.com.cn/s/blog_88a65c1b0101ixhw.html

觉得很赞
  • 评论于 2014-08-21 10:46 举报

    顶小伙儿!

  • 评论于 2014-08-21 10:47 举报

    舰长你真好,爱死你了

    , 觉得很赞
  • 评论于 2014-10-30 16:02 举报

    顶一下,好简单的样子~

  • 评论于 2014-12-05 10:51 举报

    真不错 我运行起来了 谢谢

    1 条回复
    评论于 2014-12-10 15:50 回复

    嘿嘿嘿,好好干好好干

  • 评论于 2015-01-07 23:31 举报

    有没有使用DAO的?

    1 条回复
    评论于 2015-03-26 20:50 回复

    dao就写SQL了不是?

  • 评论于 2015-04-01 11:01 举报

    为什么我的一运行就是这个错误 syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ']'

    1 条回复
    评论于 2015-06-09 11:42 回复

    语法错误啦,

  • 评论于 2015-06-05 09:50 举报

    上面所说的那种分页形式已经搞定了,只是现在又遇到了新的问题。
    如果我是使用了表单,获得表单中用户提交的数据,然后进行模糊查询,这个怎么完成分页呢?求大神指教。

    3 条回复
    评论于 2015-09-21 15:52 回复

    这么久了,您应该早就参透顺利成为大神了吧~小弟跪下求教啊

    评论于 2015-09-23 13:17 回复

    成天工作 都没什么时间学习 鸡毛

    评论于 2017-04-27 14:01 回复
  • 评论于 2015-06-09 19:30 举报

    <?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 } } } }

    是不是表有问题还是咋呢 一直纠结

    3 条回复
    评论于 2015-06-10 10:38 回复

    额, 可读性太差了

    评论于 2015-06-10 13:19 回复

    不能截图啊。抱歉。主要是views 层有值缺不能遍历出来。我把分页方法封装好了就可以了

    评论于 2015-06-30 09:53 回复

    因为这是一个对象,不是真正意义上的数组

  • 评论于 2015-07-28 11:08 举报

    为什么我的代码会报错

    Call to a member function count() on a non-object
    $pages = new Pagination(['totalCount'=>$data->count(),'pageSize'=>'2']);

    3 条回复
    评论于 2015-07-28 16:29 回复

    你用gettype()取得$data变量的类型,返回object是正确的,估计你把$data已经$data->all()了,->all() 返回的是array

    评论于 2015-08-03 15:51 回复

    确实是,但我返回的必须是全部的数据啊

    评论于 2015-08-03 15:52 回复

    好了 ,多谢指点

  • 评论于 2016-04-27 18:13 举报

    <?php
    echo 'asdasd';
    ?>

  • 评论于 2016-07-13 20:58 举报

    顶一下,继续加油

  • 评论于 2016-07-19 11:29 举报

    怎么实现无刷新啊

    3 条回复
    评论于 2016-08-03 18:12 回复

    啦啦啦啦啦啦啦

    评论于 2016-08-04 16:08 回复

    为何这么开心?

    评论于 2016-11-24 14:02 回复

    因为他终于找到了他的滑板鞋!

    觉得很赞
  • 评论于 2016-08-05 09:23 举报

    我想问一下 数组的数据格式怎么分

    5 条回复
    评论于 2016-08-05 09:39 回复

    请教一下大哥

    评论于 2016-08-05 10:53 回复

    没太明白你的意思 你的意思是 你查出来一个 大数组 然后开做分页 是吗

    评论于 2016-08-05 11:10 回复

    就像这样 $query = Apply::find()->all(); 得到的结果$query是个数组 那就不能调用$query->offset()方法 这样的话怎么进行分页 我把这个数组转成对象也不行

    评论于 2016-10-20 14:59 回复

    请问这个问题解决了吗?想向您请教一下,谢谢啦,也遇到了相同的问题

    评论于 2017-01-09 15:09 回复

    不要写-all()方法,你看清楚,上面就写到andWhere(),再分别count()和all()

    , 觉得很赞
  • 评论于 2016-10-20 15:05 举报

    <?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);?>

  • 评论于 2017-06-01 18:19 举报
    $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '5', 'params'=>['category'=>$c,'dicttype'=>$d]]);
    

    为什么我加了个参数params,就无法切换分页了呢

    1 条回复
    评论于 2017-09-14 11:38 回复

    'params'=>array_merge($_GET, ['category'=>$c,'dicttype'=>$d]) 这样加

您需要登录后才可以评论。登录 | 立即注册