0

CodeIgniter View에서 $ 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이 적합하지 않습니다. 왜냐하면 각 루프에서 $ result는 새로운 값을 취할 것이기 때문입니다. 그러므로 바람직하게는 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); 
} 

은 $ 결과 배열이 foreach이 끝난 후 var_export($result); 또는 var_dump($result); 할 내용을 확인할 수 있습니다. 이것이 당신이보기에 보내고 싶은 것인지 확인하십시오.

이제보기에 당신은 할 수 있습니다 : 당신의 도움에 대한

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

에서 수익을 문을 제거하십시오.

희망이 도움이 될 것입니다!