2016-06-29 16 views
0

マイコントローラは次のようである:CodeIgniterのビューからコントローラ内の関数を呼び出す方法は?

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

実行されると、このようなエラーが存在する:

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

マイビュー(list_view)は次のようである

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) 
{ 
    ... 
} 

コントローラ内で機能time_elapsed_stringを呼び出すことができます。

私の問題を解決するための解決策はありますか?

答えて

2

私は考えて、コントローラのメソッドを使用することはできません。ヘルパー機能が必要です。だから、

、以下のようにあなたの関数を作成し、helper.phpファイルを作成します。

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

すると以下のようにあなたのコントローラにあなたのhelper.phpファイルを含める:

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

今、あなたは意志あなたのビューでその "time_elapsed_string"関数を使用できるようにしてください。

0

ビューで$this関数を呼び出すことはできません。通常の関数とヘルパー関数を使用できます。

最高の解決策は、コントローラ内の使用機能で、表示内にはreturnの値を使用することです。

関連する問題