0

I have created the following session in codeigniter,

 $data = array(
                'employee_id' => $row->employee_id,
                'f_name' => $row->f_name,
                's_name' => $row->s_name,
                'user_name' => $row->user_name,
                'title' => $row->title,
                'is_active' => $row->is_active,
                'validated' => true
            );
            $this->session->set_userdata('user', $data);

How can I retrieve the value of employee_id from the session?

I have tried accessing it in a different function the following way but I keep on getting an error

$data = $this->session->userdata('user');

        $data['month'] = $month;
        $data['year'] = $year;

        $this->session->set_userdata('user', $data);
        $data['user_data'] = $this->session->userdata('user');

        var_dump($data);

        foreach ($user_data  as $value) {
            echo $value;
              echo 'Employee ID :'.$value->employee_id.'</br>';
        echo 'Month Session : '.$value->year.'</br>'; 

        }

Please advise on the best way to access it?

2

3 Answers 3

1

IN Controller, Please call session library

$this->load->library('session');

Now in View

$data = $this->session->userdata('user');
$employee_id = $data['employee_id'];
3
  • It's bad practice to call the session library in the view. You should call it in the controller and pass the data to the view
    – Pattle
    Feb 26, 2014 at 11:11
  • @Pattle I described above that, IN Controller Feb 26, 2014 at 12:01
  • No I mean doing $this->session->userdata('user'); in the view
    – Pattle
    Feb 26, 2014 at 12:03
0

$data['user_data'] = $this->session->userdata('user'); will return array of data.

So you can access $data['user_data']["employee_id"]

0

Make sure you are loading the session library in your controller as its not clear if you are doing that. You can do this by doing

$this->load->library('session');

or autoloading it.

Then need to change your code to

foreach ($data['user_data'] as $value) {
    echo 'Employee ID :' . $value['employee_id'] . '</br>';
    echo 'Month Session : ' . $data['year'] . '</br>'; 
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.