Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Codeigniter: Easiest way to make users details grabbed from a data base accessible by methods in any controller or method.

0

103 views

I already asked this question, but in a way that confused people.

So what I want to achieve is this:

I have a method get_user_by_email() and it is used to grab the row from the database table of a user who is logged out.

public function get_user_by_email($email_post = '') //post email grabbed from reset password controller
{
if (empty($email_post))
{
    return FALSE;
}
$query = $this->db->query("SELECT * FROM users WHERE email = '$email_post'");
$query->row();

$row = $query->row();
$user_id = $row->{'id'};
$email = $row->{'email'};
$first_name = $row->{'first_name'};
$last_name = $row->{'last_name'};

//e.g. example below is just an example and obviously wouldn't work but the variables are what I use in my mysql querys ...eg. WHERE email = $email etc

return $email;
return $first_name;
return $last_name;
return $user_id;

}

The above code doesn't work and I know this but I was giving an example of what I am doing with the data I get from the row.

I just want to some how make this available to my reset_password controller through out, then my confirm_reset_code controller and then finally my create_new_password controller.

How would I do this?

asked January 7, 2011 9:07 am CST
posted via StackOverflow

4 Answers

1
 

Add the function into a model , then you can load the model in different controllers when you need it . Please read the documentation .


Example

class User_model extends Model {

    function User_model()
    {
        parent::Model();
    }

    function get_user_by_email($email)
    {
     .....
    }
}

Save this to application/models/user_model.php , then in any of you're controller you can call :

$this->load->model('user_model');
$user_data = $this->user_model->get_user_by_email('me@example.com');

Then play with user data how you like

answered January 7, 2011 9:23 am CST
0
 

You cannot return multiple times. Just make an array, and return that.

Instead of $row = $query->row();, use $row = $query->row_array();, and then return that array.

This function should be inside a model file, which can be loaded from any controller.

answered January 7, 2011 9:23 am CST
0
 

I just made a detailed blog post about this exact topic, covering how to construct your "protected" controllers and at the same time define a global $the_user variable that is available both in all protected controllers and views.

I will try to compile a summary for this answer but until then, checkout my post.

answered January 7, 2011 9:23 am CST
0
 

Hands down the easiest way would be to store that information in a cookie/session. Typically I store the users email, name, and user_id. I of course encrypt the information stored in the cookie/session as well :)

answered January 7, 2011 11:22 am CST

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions