• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm looking for example of how to use constructor loaded controller data in my view

#1
[eluser]bill19[/eluser]
Hi,

Hi I've been reading in http://ellislab.com/codeigniter/user-gui...oader.html on how to use $this->load->vars($array) to make array elements available in views, but I can't get this working.

Here is my controller:

Code:
class Home extends Admin_Controller {

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

    function index() {  
        
        $this->load->view('user_view');
        
    }
    
    }

which extends controller:

Code:
class Admin_Controller extends MY_Controller {

    protected $the_user;

    public function __construct() {

        parent::__construct();

        if($this->ion_auth->is_admin()) {
            $data->the_user = $this->ion_auth->user()->row();
            $this->the_user = $data->the_user;
            $this->load->vars($data);
        }
        else {
            redirect('/');
        }
    }
}

my view (user_view) is:

Code:
h1>Protected View!!</h1>

<h4>Email: &lt;?= $the_user->email ?&gt;</h4>

<a href="&lt;?= site_url('user/logout') ?&gt;">logout</a>

&lt;?php echo $this->the_user->id;?&gt;
&lt;?php echo $id;?&gt;
echo $this->the_user->id;

What do I have to do to print out the value of element 'id' of the $data array?

Thanks in advance for your help,

Bill


#2
[eluser]ojcarga[/eluser]
I'm not really sure what you are doing extending those clases... but,

Are you sure the $data variable is really an <b>associative</b> array??

"This function takes an associative array as input and generates variables using the PHP extract function. "

Did you try using
Code:
$this->load->view('user_view', $data);
intead of
Code:
$this->load->vars($data);
??

#3
[eluser]CroNiX[/eluser]
$data needs to be an associative array, you created an object. What you're doing with load::vars() should be fine once you pass it the data in the correct format.

#4
[eluser]bill19[/eluser]
Thanks for the explanation Cronix.

Ojcarga, I'm trying to follow http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY

Cheers,

Bill


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.