Reputation: 577
I have created a session and assigned User_id
to the Session Variable. When i use print_r();
it shows me the the session variable data in an associative array but when i use foreach it gives me many error messages.
Kindly check it and guide me what i am doing wrong here.
print_r($session_dataa= $this->session->all_userdata());
Session dataArray ( [session_id] => 5943ecd761c336e70240b25d37b054b8
[ip_address] => ::1
[user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0)
Gecko/20100101 Firefox/28.0
[last_activity] => 1397982790
[user_data] => [41] => )
I want to grab [user_data]=> [41]
and store in student_id
variable. To accomplish this i used foreach loop, but its giving me following error message.
$session_data= $this->session->all_userdata();
foreach ($session_data as $data)
{
$student_id = $data->user_data;
}
Severity: Notice
Message: Trying to get property of non-object
Filename: views/students.php
Line Number: 32
This is how i am assigning User_id to session data. mycontroller.php
//[[session]]
$data['students_data']= $this->loginmodel->student_profile($currentId);
$this->load->library('session');
$this->session->set_userdata($currentId);
Upvotes: 2
Views: 3733
Reputation: 1274
You are not associating current_id
with any variable, The Correct way to assign Values to a session variable is given below;
Controller
$this->load->library('session');
$this->session->set_userdata(array(
'currentId' =>$current_id
));
View
$student_id = $this->session->userdata('currentId');
Upvotes: 4
Reputation: 6014
To get your session variable simply do
$this->session->userdata('user_data');
When you access a session variable using the session class like this it will automatically check to see if its set or not so you won't get any errors.
Upvotes: 2
Reputation: 778
Try this
$session_data= $this->session->all_userdata();
$student_id = $session_data[user_data];
OR
$session_data= $this->session->all_userdata();
foreach ($session_data as $data)
{
$student_id = $data[user_data];
}
Upvotes: 0