Php Can';t从CodeIgniter下的foreach循环检索值
我无法在CodeIgniter视图中从$info(如下所述)检索值 以下是场景: 我解释了代码中的所有内容Php Can';t从CodeIgniter下的foreach循环检索值,php,codeigniter,foreach,codeigniter-2,variable-assignment,Php,Codeigniter,Foreach,Codeigniter 2,Variable Assignment,我无法在CodeIgniter视图中从$info(如下所述)检索值 以下是场景: 我解释了代码中的所有内容 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. f
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.
请从中删除返回语句
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);
现在$info可以在视图中访问
希望这对你有帮助 请从中删除返回语句
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);
现在$info可以在视图中访问
希望这对你有帮助 在
foreach
内部使用$result
是不合理的。因为在每个循环中,$result将接受一个新值。因此,最好将其用作数组
,然后将其传递给视图。此外,您不应该在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);
}
要检查$result数组执行的操作,请执行var\u导出($result)代码>或变量转储($result)代码>在foreach
结束后。并确保这是您要发送到视图的内容
现在,在您看来,您可以执行以下操作:
<?php foreach ($info as $something):?>
//process
<?php endforeach;?>
//过程
在内使用$result
foreach
是不合理的。因为在每个循环中,$result将接受一个新值。因此,最好将其用作数组
,然后将其传递给视图。此外,您不应该在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);
}
要检查$result数组执行的操作,请执行var\u导出($result)代码>或变量转储($result)代码>在foreach
结束后。并确保这是您要发送到视图的内容
现在,在您看来,您可以执行以下操作:
<?php foreach ($info as $something):?>
//process
<?php endforeach;?>
//过程