php codeigniter所有其他功能的单一功能

时间:2013-11-17 17:40:28

标签: php codeigniter

这是我的问题的一些细节。

controller:  
function get_info()  
{  
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');  
$msg_data['msg']=$this->message_model->get_user_msg($user_id); 
$this->load->view('header',$msg_data);  
$this->load->view('content');  
$this->load->view('footer');     
}  

function others_goes_here()  
{  
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');  
$msg_data['msg']=$this->message_model->get_user_msg($user_id); 
LOAD OTHER VIEWS.....    
}
model:
function get_user_msg($user_id)
{
QUERY GOES HERE......
}  

现在我想要实现的是如何只调用一次get_user_msg而不调用所有其他函数。希望你们能帮我这个,
提前谢谢。

2 个答案:

答案 0 :(得分:2)

您创建一个类变量并从构造函数运行get_user_msg

protected $msg_data;

function __construct() {
     parent::__construct();
     $this->load->model('message_model'); 
     $user_id = $this->session->userdata['logged_in']['userid']
     $this->msg_data = $this->message_model->get_user_msg($user_id);
}

function function get_info() {
     $this->load->view('my_view', $this->msg_data);
}

答案 1 :(得分:1)

使用构造函数

public $user_msg;
function __construct()
{
    parent::__construct();
            $this->load->model('message_model');
            $user_id = $this->session->userdata['logged_in']['userid']
            $this->user_msg=$this->message_model->get_user_msg($user_id);

}
function AllOtherFunctions(){
     $this->user_msg; //you will have it ready every where
}