如何从CodeIgniter中的视图调用控制器中的函数?

时间:2016-06-29 23:07:26

标签: php codeigniter

我的控制器是这样的:

td

我的观点(list_view)是这样的:

(//td[1])[1]

执行时,会出现如下错误:

  

致命错误:调用未定义的方法   EX_Loader :: time_elapsed_string().....

似乎无法在控制器中调用函数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) { ... }

解决我的问题的任何解决方案?

2 个答案:

答案 0 :(得分: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”函数。

答案 1 :(得分:0)

你不能在视图中调用$this函数,你可以使用普通函数和辅助函数。

最佳解决方案是在控制器中使用函数并在视图中使用return值。

相关问题