1

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

3
  • Is the index function a member of the same class? Commented May 30, 2017 at 15:42
  • Yes it is. And, thanks....The answer below by MrCode worked out Commented May 31, 2017 at 6:39
  • You should mark it as the accepted answer then so that it's flagged as resolved. Commented May 31, 2017 at 20:11

1 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you brother. This one worked out though I had to change the way I call it from index function as $staff_no=$this->staff_no; to $staff_no['staff_no']=$this->staff_no;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.