无法从CodeIgniter的foreach循环中检索值
问题描述:
我无法从CodeIgniter视图中的$ info(如下所述)中检索值。无法从CodeIgniter的foreach循环中检索值
这里是场景: 我解释了所有的代码。
function info() {
{...} //I retrieve results from database after sending $uid to model.
$dbresults = $this->my_model->get_info($uid); //Assume that this model returns some value.
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$results = $this->my_model->show_info($info);
return $results; //This is my final result which can't be achieved without using $row->address. so first I have to call this in my controller.
}
// Now I want to pass it to a view
$data['info'] = $results;
$this->load->view('my_view', $data);
//In my_view, $info contains many values inherited from $results which I need to call one by one by using foreach. But I can't use $info with foreach because it is an Invalid Parameter as it says in an error.
答
使用$result
里面foreach
是不合理的。因为在每个循环中$结果都会有一个新的值。因此,最好将它用作array
,然后将其传递给您的视图。此外,你不应该在foreach
内使用return
。
function info() {
{...} //I retrieve results from database after sending $uid to model.
$dbresults = $this->my_model->get_info($uid); //Assume that this model returns some value
$result = array();
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$result[] = $this->my_model->show_info($info);
}
// Now I want to pass it to a view
$data['info'] = $result;
$this->load->view('my_view', $data);
}
检查什么$结果数组做var_export($result);
或var_dump($result);
的foreach
结束后。并确保这是你想发送给你的观点。现在
,在你看来,你可以这样做:
<?php foreach ($info as $something):?>
//process
<?php endforeach;?>
答
请从
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$results[] = $this->my_model->show_info($info);
// return $results; remove this line from here;
}
$data['info'] = $results; // now in view access by $info in foreach
$this->load->view('my_view', $data);
删除回报语句现在$信息可以在视图访问。
希望这会帮助你!
感谢您的帮助。 – Zim3r