Codeigniter - using session data in view Codeigniter - using session data in view codeigniter codeigniter

Codeigniter - using session data in view


Controller code:

public function index() {    ...    $data['first_name'] = $this->session->userdata('first_name');    ...}

View code:

<?= $first_name; ?>

OR, if you want to pass the data like you currently are, you would access the view data userdata as an array, not a function, ie:

<?= $userdata['first_name'];?>


As I mentioned in the comments , when you assign a value to array $data and pass it through Codeigniter view loader , the data will be available in the view part with variable names same to the array indexes.
for example when you do this:

$data['name'] = "bla bla";$this->load->view('some_view', $data);  

then the $name variable in the view will contain your passed value. In your case you are assigning an array to $data['userdata']. so the $userdata in the view will an array too.

So do like this:

echo $userdata['firstname'];echo $userdata['lastname'];  

in the view part.