我想将这些值从控制器显示到视图
以下是我的控制器功能片段:
$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)
答案 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;
}
?>