• 0
Votes
name
name Punditsdkoslkdosdkoskdo

How to Codeigniter - using session data in view

I have a users first and last name stored in session data and I want to echo it out in the view so they can see who they are logged in as. I can't seem to get the data to pass. I'm sure this is something easy but I'm pretty new to CI and the whole MVC thing.

Controller Code:

public function index() {
    if($this->session->userdata('admin_signed_in')){
    $this->load->model('dashboard_model');
    $data['userdata'] = $this->session->userdata;
    $data['main_content'] = 'dashboard/dashboard';  
    $this->load->view('common/template', $data);
    } else {
    redirect('signin');
    }

}

View Code:

<?php echo $userdata('first_name') ?>  <?php echo $userdata('last_name') ?>

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'];?>
  • 1
Reply Report