tarja
tarja

Reputation: 28

Codeigniter: passing session userdata from controller to view

So this is a small problem, but after research still can't make this work. I want to pass "username" variable from session user data. From my controller to view. Here is my code:

Controller:

    function members_area()
{
    $user['username'] = $this->session->userdata('username');
    $data['main_content'] = 'members_area';
    $this->load->view('includes/template', $data, $user);
}

And in view I use this:

<?php echo "$username" ?>

EDIT: I solved it in a more simple way. Because session userdata is available global in every page while user is logged in, there is no need to pass it like this.

I just made this line in my view page:

<?php echo $name = $this->session->userdata('username');

Upvotes: 0

Views: 8323

Answers (2)

David
David

Reputation: 3822

The third parameter of $this->load->view is True or False - not another variable.

The Documentation provides a fantastic overview of how views work.

Do this:

$this->load->view('includes/template', $data); 

And then in the view:

<?php echo $username; ?>

You might also consider using a real templating plugin for CodeIgniter, if you plan to use a template.

Good luck!

Upvotes: 0

ldiqual
ldiqual

Reputation: 15365

You just have to populate only $data:

// Controller
function members_area()
{
  $data['username'] = $this->session->userdata('username');
  $data['main_content'] = 'members_area';
  $this->load->view('includes/template', $data);
}

// View
<?= $username ?>

Upvotes: 2

Related Questions