2016-06-29 4 views
0

내 컨트롤러이 같다 :CodeIgniter의 뷰에서 컨트롤러의 함수를 호출하는 방법은 무엇입니까?

public function index() 
{ 
    $data_login = $this->session->userdata('login'); 
    $id = $data_login['customer_id']; 

    $this->data['notification'] = $this->get_data($id);  

    $this->load->view('notification/list_view', $this->data); 
} 

public function time_elapsed_string($datetime, $full = false) 
{ 
    ... 
} 

내보기 (list_view하면)이 같다 :

Fatal error: Call to undefined method EX_Loader::time_elapsed_string().....

: 실행되면

<table class="table table-striped table-advance table-hover"> 
    <tbody> 
    <?php 
     foreach ($notification as $key => $value) 
     { 
    ?> 

     <tr class="unread" data-messageid="1"> 
      <td class="view-message "> 
       <?php 
        echo '<a href='.base_url().'notification/notif/'.$value['notification_id'].'>'.$value['notification_message'].'</a><br>'; 
       ?> 
      </td> 
      <td class="view-message text-right"> <?php echo $this->time_elapsed_string($value['notification_created_date']); ?> </td> 
     </tr> 

    <?php 
     } 
    ?> 
    </tbody> 
</table> 

은 이와 같이 에러가 존재

컨트롤러에 time_elapsed_string 함수를 호출 할 수 있습니다.

내 문제를 해결하려면 어떤 해결책이 있습니까?

답변

2

저는 컨트롤러의 방법을 사용할 수 없다고 생각합니다. 도우미 기능이 있어야합니다.

$this->load->helper('helper'); 
이제

당신은 것입니다 :

if (! function_exists('time_elapsed_string')){ 
    function time_elapsed_string($datetime, $full = false){ 
     // do your calculation 
     return 'something'; 
    } 
} 

그런 다음 아래와 같이 컨트롤러에 당신의 helper.php 파일을 포함 :

그래서, 다음 helper.php 파일을 만들고 아래 함수를 만들 보기에서 해당 "time_elapsed_string"함수를 사용할 수 있어야합니다.

0

보기에서 $this 기능을 호출 할 수 없으면 일반 기능과 도우미 기능을 사용할 수 있습니다.

가장 좋은 해결책은 컨트롤러의 사용 기능이며,보기에는 return 값을 사용하십시오.

관련 문제