Welcome Guest, Not a member yet? Register   Sign In
Can't send data from controller to view - variable scope problem?
#1

[eluser]duartix[/eluser]
Cheers.

This is most probably a noob problem, but I'm having difficulties passing data from a controller to a view. I've copied the philosophy of the news section tutorial and wrote the controller as follows:

application/controllers/home.php

Code:
<?php

class Home extends CI_Controller {

    public function __construct() {

        parent::__construct();

        // Initializes connection data
        $data['connection'] = array(
            'logged' => TRUE,
            'username' => 'duarte',
        );
        
        if ($data['connection']['logged']) {
            $this->load->view('templates/logged',$data);
        } else {
            $this->load->view('templates/login');
        }
    }
}

and the view is:

application/views/logged.php

Code:
<div id="login_form">
    &lt;form method="post" acti&gt;
        Connected as:
        &lt;?php echo $data['connection']['username'] ?&gt;  
    &lt;/form&gt;
</div>

The view tells me: Undefined variable: data

From what I've gathered about PHP, the scope of $data is local to the Home controller class, so it would make sense that the view wouldn't see it, but then again I'm passing it explicitly... If I change $data to $GLOBALS it will work, but it looks very dirty from an abstraction point of view.

So why does it work on the tutorial? Are the tutorial's view and controller married in a way that I am missing and share their little dirty data? How else can I send that data?

Thanks In Advance.
Duarte Bruno.


Messages In This Thread
Can't send data from controller to view - variable scope problem? - by El Forum - 08-30-2013, 02:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB