将控制器构造函数值设置为全局 codeigniter

发布于 2024-12-24 22:44:25 字数 830 浏览 1 评论 0原文

我试图从数据库中获取未读帐户总数,该值被分配给 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-12-31 22:44:25

您首先需要使用变量作用域将 $data 设为函数外部的变量。可以是私有的或公共的。在这种情况下,我将其设为私有。

这是一个快速修改:

private $data = array();

function __construct()
    {

    parent::__construct();
    $this->load->model('process_model');
    $this->data['headbody']='includes/header';
    $this->data['head'] = $this->process_model->loadnew($this->session->userdata('id'));

    }


function invform()
    {
        $this->load->model('slave');
        $this->data['body']='slave-account';

        $this->data['questions'] = $this->slave->loadq($this->uri->segment(3));

        $this->load->view('includes/template',$this->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:

private $data = array();

function __construct()
    {

    parent::__construct();
    $this->load->model('process_model');
    $this->data['headbody']='includes/header';
    $this->data['head'] = $this->process_model->loadnew($this->session->userdata('id'));

    }


function invform()
    {
        $this->load->model('slave');
        $this->data['body']='slave-account';

        $this->data['questions'] = $this->slave->loadq($this->uri->segment(3));

        $this->load->view('includes/template',$this->data);
    }

Notice the $this->data, instead of $data. When we're accessing variables within the same class, but outside of the function, we use $this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文