将控制器构造函数值设置为全局 codeigniter
我试图从数据库中获取未读帐户总数,该值被分配给 $data['head']
我想让 $data['head'] 全局可用,这样它就会自动加载到模板中并显示在标题上。
最好的方法是什么?
下面是我的控制器
function __construct()
{
parent::__construct();
$this->load->model('process_model');
$data['headbody']='includes/header';
$data['head'] = $this->process_model->loadnew($this->session->userdata('id'));
}
function invform()
{
$this->load->model('slave');
$data['body']='slave-account';
$data['questions'] = $this->slave->loadq($this->uri->segment(3));
$this->load->view('includes/template',$data);
}
视图
$this->load->view($head);
$this->load->view($body);
$this->load->view('includes/footer');
Im trying to get the total unread accounts from the database, the value is assigned to $data['head']
I want to make the $data['head'] available globally so it will be automatically loaded into the template and displayed on the header.
What is the best way to do this?
Below is my controller
function __construct()
{
parent::__construct();
$this->load->model('process_model');
$data['headbody']='includes/header';
$data['head'] = $this->process_model->loadnew($this->session->userdata('id'));
}
function invform()
{
$this->load->model('slave');
$data['body']='slave-account';
$data['questions'] = $this->slave->loadq($this->uri->segment(3));
$this->load->view('includes/template',$data);
}
View
$this->load->view($head);
$this->load->view($body);
$this->load->view('includes/footer');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您首先需要使用变量作用域将 $data 设为函数外部的变量。可以是私有的或公共的。在这种情况下,我将其设为私有。
这是一个快速修改:
请注意 $this->data,而不是 $data。当我们访问同一类内但函数外部的变量时,我们使用$this。
You first need to make $data into a variable outside the function, using variable scope. Can be private or public. I made it private in this case.
Here's a quick revision:
Notice the $this->data, instead of $data. When we're accessing variables within the same class, but outside of the function, we use $this.