【问题标题】:YII PHP nested foreach errorYII PHP嵌套foreach错误
【发布时间】:2015-03-29 07:37:13
【问题描述】:

我这里有一些错误 我想使用嵌套的foreach 组合来自 2 个查询的数据,并将数据保存在一个名为 result 的数组中,但在这部分代码中存在错误Trying to get property of non-object'id_product'=>$detail_result->ID, 谁能帮帮我?

我还是新手,使用 yii 框架 php

这是我的代码

$result = 数组();

        $criteria = new CDbCriteria;
        $criteria->condition = 'date_time >= :start and date_time <= :end';
        $criteria->order = 'date_time';
        $criteria->params = array(':start' => $_POST['tanggal']['start'] ,':end' => $_POST['tanggal']['end']);

        $checkIn = CheckIn::model()->findAll($criteria);

        foreach($checkIn as $entry) {
            $sql = "select * from check_in_detail where ID_check_in = $entry->ID";
            $detail_results = Yii::app()->db->createCommand($sql)->queryAll();

            foreach($detail_results as $detail_result)
            {
                 $result [] = array(
                     'tanggal'=>$entry->date_time,
                    'id_product'=>$detail_result->ID,
                     'total'=>$detail_result->total,
                     'id_distributor'=>$entry->iDDistributor->name,
                     'other'=>$entry->description,
                 );
             }
         }

有人可以帮忙吗? 谢谢

【问题讨论】:

    标签: php yii foreach frameworks nested


    【解决方案1】:

    问题在于您访问$detail_result 变量,

    函数queryAll() 将返回一个数组数组,而不是对象数组!

    将您的代码更改为以下:

                 $result [] = array(
                     'tanggal'=>$entry['date_time'],
                     'id_product'=>$detail_result['ID'],
                     'total'=>$detail_result['total'],
                     ...  
                 );
    

    (顺便说一句,您可以通过 var_dump 变量来查看它包含的确切内容)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多