Maurice
Maurice

Reputation: 127

CodeIgniter persistent data across multiple views

I am building a webapp using CodeIgniter but I am new to it. I have front page with various sections using different arrays of data from a database.

What I want is to load the data from a base controller once and extend this controller and re-use this data across multiple views.

What I do not want to do is repeat the same database call across the other controllers.

Is this even possible?

What I have now is:

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
      parent::__construct();
      $data['somevar'] = $this->some_model->get();
    }

}

I want to use $somevar in a foreach loop in a section of the page.

Note: I am using a template library.

Any help will be appreciated.

Upvotes: 0

Views: 412

Answers (1)

swatkins
swatkins

Reputation: 13630

What you need to understand is that every refresh of the page loads the entire CI application again (so that constructor call will happen for each page load). Therefore, there is no persistence in data calls from one view to the next (given that you're indeed talking about a new page).

You'd have to come up with another way to persist the data - either in a cookie, client storage, session, or server-side cache. That way, the data would be available across separate page views without being requested over and over.

Upvotes: 1

Related Questions