2013-01-19 31 views
0

我無法從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. 

回答

3

使用$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;?> 
+0

感謝您的幫助。 – Zim3r

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); 

刪除回報語句現在$信息可以在視圖訪問。

希望這會幫助你!