Welcome Guest, Not a member yet? Register   Sign In
Is this a good approach?? Phil Sturgeon Template Library
#1

[eluser]NachoF[/eluser]
I want my site to show me the name of the user thats using the site on every page.. and its seems to be that it is a little redundant having to send the user as data everytime Im going to use
Code:
$this->template->build();
The only thing I can come up with is this. on every controller, instead of calling
Code:
$this->template->build("someview", $data);
I will call
$this->myviewbuilder("someview", $data);

and in My_Controller
Code:
function myviewbuilder($view="",$data="")
        {
            $data["user"]=$this->current_user();
            $this->template->build($view, $data);

        }

so now in my layout file I can always use
Code:
$user

I cant think of a better way to do this.. please help.
#2

[eluser]falkencreative[/eluser]
Couldn't you just set a session variable to store that information? And then request that user data in your view file?
#3

[eluser]NachoF[/eluser]
Basically what I want is a sidebar div where if the user is logged in it shows his name. otherwise it shows a login form
Having to set the user object the way you are sugesting I would have to do something like this in my master view... which doesnt look like very good practice cause its permoring a database query inside the master view
Code:
<div>
&lt;?php
$u=new User();
$id=$this->session->userdata('user_id');
$u->get_by_id($id);
?&gt;
&lt;?php if(!$user->exists()):?&gt;
&lt;?=form_open("login", "login")?&gt;
Login: &lt;?=form_input("login")?&gt;
<br />
Password: &lt;?=form_password("password")?&gt;
<br />
&lt;?=form_submit("Submit","Submit")?&gt;
&lt;?=form_close()?&gt;
&lt;?php else:?&gt;
&lt;?=$user->name.", ".anchor("logout","logout")?&gt;
&lt;?php endif;?&gt;
</div>
#4

[eluser]NachoF[/eluser]
Ok, this seems to be a better way.
http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY
#5

[eluser]falkencreative[/eluser]
Seems to me that you could do this:

-- when a user logs in, set a session variable storing his name and id

Thus, in your sidebar div, you could do something like this:

Code:
if (isset($this->session->userdata('user_name')))
{
    // show hello message (again, using the session variable), loutout link
}
else
{
   // show login form
}

This way, you minimize database calls. With your code, my impression is that you are checking the database on each page load to find the username, I believe you'll also run into an error if $this->session->userdata('user_id') isn't set and you try to access it (haven't tested it, but that's my impression.)




Theme © iAndrew 2016 - Forum software by © MyBB