Welcome Guest, Not a member yet? Register   Sign In
How to access session directly in /view files ? $this->session->userdata();
#1

[eluser]web_developer[/eluser]
I have some common files which includes in all pages of the site
like Footer Part, Left Panel part, etc..

Now I have one session variable $this->session->userdata('user');
it's working properly in controller. But when I try to echo in Footer or lefpanel bar then it's not printing.

Why it's not coming? I could not access session variable directly in view pages?

This files are including entire whole project. So what is the best solution to access session? to directly in /view?
#2

[eluser]coolamit[/eluser]
"$this" object is not available in views. So you need to pass it in the data array when loading the view like this:

Code:
$data = array();
//... load up variables in data array to pass into view
$data['objCI'] = $this;
$this->load->view('myview', $data);

& then you can use it like this in view

Code:
$objCI->session->userdata('myvar');

Or you can just pass the session object to the view like:

Code:
$data = array();
//... load up variables in data array to pass into view
$data['objSession'] = $this->session;
$this->load->view('myview', $data);

& then you can use it like this in view

Code:
$objSession->userdata('myvar');

Or you can get the CI instance in the view like this

Code:
$objCI =& get_instance();
//now CI object can be used
$objCI->session->userdata('myvar');
#3

[eluser]web_developer[/eluser]
Thanks a lot, it's working now. Smile




Theme © iAndrew 2016 - Forum software by © MyBB