1

我有这个问题:如何在每个页面的 codeIgniter 中加载函数并从中获取数据或变量以显示在视图中?
我有很多控制器,每个控制器都有很多功能。每个函数都将主视图作为模板加载并加载自己的视图。主视图有动态数据,我从数据库中推送它。
所以我需要在主视图中自动显示数据库中的一些数据。怎么做?

4

3 回答 3

0

if you want to use your function inside views. use CI Helpers

Codeigniter Helpers

create your helper function

function get_data() {

 // and use CI instance
 $CI = & get_instance();

 // now you can call your models, libraries. etc. in the helper
 $data = $CI->your_model->your_model_funcion();

 return $data;
}

and you can call your helper function in controllers, views, models. etc...

get_data();
于 2013-03-22T07:49:01.603 回答
0

你可以使用这个。根据你的改变它

function index()
{
    $data['list']       =   $this->some_model->show_data();
    $data1['content']   =   $this->load->view('some_view',$data,true);
    $this->load->view('template',$data1);
}

内容在您的主模板中,您将在其中传递您的数据以显示在模板中

于 2013-03-22T07:04:29.517 回答
0

我不确定您到底要问什么,但如果您希望将功能自动加载到任何控制器

然后创建Mycontroller.php一个application/core

class Mycontroller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        // here goes your function
    }
}

然后扩展你必须的每个控制器Mycontroller而不是CI_Controller

于 2013-03-22T07:29:10.737 回答