0

我无法在 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.


    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.
4

2 回答 2

3

$result在里面使用foreach是不合理的。因为在每个循环中 $result 都会取一个新值。因此,最好将其用作 anarray然后将其传递给您的视图。此外,您不应该使用returninside 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

$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_export($result);var_dump($result);foreach. 并确保这是您要发送到您的视图的内容。

现在,在您看来,您可以执行以下操作:

<?php foreach ($info as $something):?>

//process

<?php endforeach;?>
于 2013-01-19T12:07:46.050 回答
1

_

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 可以在视图中访问。

希望对你有帮助!

于 2013-01-19T11:46:34.637 回答