1

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');

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.