Ayub
Ayub

Reputation: 25

Call variables in construct function from other functions in Codeigniter

I have the code below below

public fucntion __construct(){
parent::__construct();
if($this->session->userdata('logged_in'))
        {
     $session_data = $this->session->userdata('logged_in');
     $data['staff_no'] = $session_data['staff_no'];
     $data['staff_email'] = $session_data['staff_email'];
     $data['staff_fname'] = $session_data['staff_fname'];
     $data['staff_lname'] = $session_data['staff_lname'];
     $data['staff_level'] = $session_data['staff_level'];
        }
else
{
redirect('login','refresh');
}

Now, i have another function below

public function index()
{
$staff_no=$this->staff_no;

    $this->load->view('header');
    $this->load->view('accounts/left-nav');
    $this->load->view('accounts/top-nav');
    $this->load->view('accounts/home');
    $this->load->view('footer',$staff_no);
}

$staff_no variable above is meant to get the $data['staff_no']=$session_data['staff_no']; located in the __construct function but getting the error as undefined variable staff_no Kindly help

Upvotes: 1

Views: 104

Answers (1)

MrCode
MrCode

Reputation: 64526

You haven't defined $this->staff_no, you've set a local variable $data['staff_no'] in the constructor.

If you want to access the variable in other methods of the class, you need to store it in a property:

Constructor:

$this->staff_no = $session_data['staff_no'];

Upvotes: 2

Related Questions