Welcome Guest, Not a member yet? Register   Sign In
Passing vars from MY_Controller to child controllers and views
#1

[eluser]suntrop[/eluser]
Hi there.

I want to have some variables from a config table in my DB. Since I need them everywhere I'm loading them in MY_Controller. But somewhere between the controller and the view they get lost. print_r writes blank array to my screen.

MY_Controller.php [EDIT: code is in constructor]
Code:
$this->load->model('global_model');
$config_db = $this->global_model->get_config_db();

global_model.php
Code:
function get_config_db()
    {
        $query = $this->db->get('config');
        return $query->result_array();
    }

Welcome Controller (welcome controller extends public controller which extends MY_controller)
Code:
$data['app_config'] = $this->config_db;  // this should come from MY_controller
$data['content'] = 'welcome';
$this->load->view('includes/template_public', $data);

... and in my view:
<pre>&lt;?php print_r($app_config); ?&gt;</pre>

But all I see is
Array
(
)

How do I access the vars from the MY_Controller in a subset controller?

Hope someone can please help.
#2

[eluser]osci[/eluser]
Code:
class MY_Controller extends Controller
{
public $config_db;

function __construct()
{
   parent::__construct();
   $this->load->model('global_model');
   $config_db = $this->global_model->get_config_db();
}
#3

[eluser]suntrop[/eluser]
Thanks, but there is no effect at all.

If I don't declare a variable, it is the same as public, isn't it? I am just starting to write OOP.
#4

[eluser]danmontgomery[/eluser]
Code:
class MY_Controller extends Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('global_model');
        $this->config_db = $this->global_model->get_config_db();
    }

}

In PHP, yes... If you don't declare class members as otherwise they are public.
#5

[eluser]suntrop[/eluser]
:-)

That's it …
$this->config_db = $this->global_model->get_config_db();

Thanks a lot!




Theme © iAndrew 2016 - Forum software by © MyBB