0

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');

2 Answers 2

2

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 ?>
Sign up to request clarification or add additional context in comments.

Comments

0

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!

2 Comments

ok, this way doesn't worked for me somehow, so I made it work in other way, changing $this line to $this->load->view('members_area', $user); but I can't get all my style of page this way
Read the link I posted, and then read about templating plugins. It will make your life much easier with CodeIgniter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.