Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Loading vars for use in header/footer views in MY_Controller
#1

[eluser]Unknown[/eluser]
I have legacy PHP, which queries database and isn't CI at all, in my footer view file. I want to replace this with a MY_Controller which loads the vars to be used later in the views.

I've created a MY_Controller

Code:
class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $templatevars['testlol'] = 'yes';
        $this->load->vars($templatevars);
    }
}

And extended all my controllers, so they use MY_Controller,

Code:
class Page extends MY_Controller {

}

And in my footer view
Code:
print $testlol;

But then I get an undefined variable of $testlol.

What am I doing wrong?
#2

[eluser]eoinmcg[/eluser]
you should probably read up about scope in php classes

declaring $templatevars in the constructor means that it is only accessible within the constructor.

what you want to do is something like this:
Code:
class MY_Controller extends CI_Controller {

    public  $templatevars = array();

    public function __construct()
    {
        parent::__construct();

        $this->templatevars['testlol'] = 'yes';
        $this->load->vars($this->templatevars);
    }
}
#3

[eluser]Unknown[/eluser]
Thank you so much!

Its the first project I've done in CI, and I'll never be going back to plain PHP.




Theme © iAndrew 2016 - Forum software by © MyBB