Welcome Guest, Not a member yet? Register   Sign In
Displaying a User's Information After Hes Logged In
#1

[eluser]JayArias[/eluser]
I'm running into a bit of a problem. I LOVE code igniter with capital letters but, it has it's downs and those are that I can never get how to globally display a logged in users information I've tried

Quote: @Colin Williams
I don’t think you need a “getter” for every little data object. And I’m not sure why you want to avoid a model? Embrace the Holy Trinity

Consider this code in a Controller constructor:

$this->load->model('user_model');
$this->load->vars('cur_user', $this->user_model->get_current_user);
// Imagine get_current_user() returning either a user object
// from the database or a dummy object of an anonymous user.
Now the variable $cur_user is available to all of your views, so you can echo it anywhere

<h2>&lt;?php print $cur_user->first_name ?&gt;</h2>
Signature
Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

I've also just tried
Code:
$this->template->write_view('content', 'templates/admin/content', $data);
. It worked! Big Grin .... Sad but the only issue with that is ... I wont have the luxury of echoing data from a model into a view all the time. So I basically need to come up with a way that I can code within a View &lt;?= $user->email;?&gt; and have it echo [email protected].


Therefore I have had to turn around and use another php framework that is not mvc which I HATE. Can some one please help me, I already now how to get the user to sign in I just need to call their information from the model. I don't want to use 3rd party code as the code I wrote I already know what it does and I don't have to append things here and there, but it's always and option.

Long story short... Some one please help me :'(
#2

[eluser]Ben Edmunds[/eluser]
Your problem isn't CodeIgniter or MVC, you're not understanding/using MVC correctly.

Say you have a users_model with a method called get_user($user_id) that returns the user object with

Code:
return $query->result();


In your controller you can do

Code:
$this->load->model('users_model');
$data['user'] = $this->users_model->get_user($user_id);
$this->template->write_view('content', 'templates/admin/content', $data);

Then in your view you have the complete user object available.

If you need it need it on every view you can use a MY_Controller or something similar.
#3

[eluser]JayArias[/eluser]
I completely understand what you're saying and .. well thats how I am set up currently. The only problem is that if needed I can't echo anything else to that View .

hence:

Code:
$this->load->model('users_model');
$data['user'] = $this->users_model->get_user($user_id);
$this->template->write_view('content', 'templates/admin/content', $data);

I will be SOL if I needed to do the following
Code:
$this->load->model('messages');
$data['messages'] = $this->pm_model->get_last10();
$this->template->write_view('content', 'templates/admin/content', $data);
#4

[eluser]Ben Edmunds[/eluser]
No you won't. Use $this->data instead of $data, So

Code:
$this->data['user'] = $this->users_model->get_user($user_id);

and

Code:
$this->data['messages'] = $this->pm_model->get_last10();

and don't write the view until you have everything you need in $this->data. Then once you have everything do

Code:
$this->template->write_view('content', 'templates/admin/content', $this->data);
#5

[eluser]JayArias[/eluser]
You Sir.. are a genius.
#6

[eluser]Ben Edmunds[/eluser]
Hahaha, hardly.

Glad to help though.


Enjoy!




Theme © iAndrew 2016 - Forum software by © MyBB