• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__construct passing data?

#1
Question 
Hi,

I'm trying to do a 'get' query inside the __construct of my controller to get some users information. I have a general 'core' layout which needs this information.  When trying to do a print_r($data) inside my core view, it returns blank. Could someone point out what im doing wrong?

PHP Code:
class Staff extends MY_Controller {
 
   
    
/*******************************************************
     * CONSTRUCT
     *******************************************************/
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('staff_model');
 
       $data['session'] = $this->get_session_information();
 
       print_r($data['session']); // this returns my data as expected
 
   

However when in my view.... nothing is returned. I'm assuming it is because $data is not passed into the view at this point in the construct. I dont want it to pass the view at this stage. I therfore think that data needs to somehow be made public, so it can be built up inside the controller at any stage. Could someone clarify what i'm doing incorrectly?

PHP Code:
<pre>
<?
php  print_r($session); ?>
<?php  print_r
($data); ?>
</pre> 

Thanks,

MoFish
Reply

#2
$data is a local variable of your constructor. You need to make a member variable of your controller class if you want to use it somewhere else in other functions of your controller.

PHP Code:
class Staff extends MY_Controller {

 
   private $data;

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('staff_model');
 
       $this->data['session'] = $this->get_session_information();
 
   }

 
   public function index()
 
   {
 
       $this->load->view('hello'$this->data);
 
   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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