从控制器显示视图中的变量

时间:2016-12-24 08:41:46

标签: php codeigniter

我想将这些值从控制器显示到视图

以下是我的控制器功能片段:

        $i = 0;

        foreach($data['main']->result() as $list):

           $list_id = $list[$i]['check_id'];

           //I concatinate the 'extra' and $i so it would have a unique name
           $data['extra'.$i] = $this->MyModel->getExtra($list_id);

           $i++;

        endforeach;

        //I store it on data so I would know the max number in the view
        $data['max'] = $i;

        $this->load->view('Main/viewList',$data);

那里没有错误。如果所有变量都成功存储,也检查变量。

我现在想在视图上显示它。

                <?php
                  for($i=0; $i<$max; $i++) {

                    foreach(($extra.$i)->result() as $list):
                      echo $list->name;

                    endforeach;
                  }
                ?>

$extra.$i对视图无效。它的解析错误为unexpected '->' (T_OBJECT_OPERATOR)

1 个答案:

答案 0 :(得分:2)

改变
<?php
     for($i=0; $i<$max; $i++) {
         foreach(($extra.$i)->result() as $list):
                echo $list->name;
                endforeach;
     }
?>

<?php
         for($i=0; $i<$max; $i++) {
             $extra = ${'extra' . $i};
             foreach($extra->result() as $list):
                    echo $list->name;
                    endforeach;
         }
    ?>