开发者

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

开发者 https://www.devze.com 2023-02-03 14:29 出处:网络
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 u

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?

NEW MODEL

class Get_user_by_email extends CI_Model {
    public function __construct() {
        parent::Ci_Model();

    }

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

        }

}

NEW CONTROLLER

<?php

class Account extends FH_Controller {
    public function __construct() {
        parent::__construct();

        $this->load->model('get_user_by_email');


    }

    public function reset_password()
    {
        $this->load->view('reset_password');
        if ($this->get_user_by_email->process($this->input->post('e-mail')))
        {
        $query = $this->get_user_by_email->process();
        print_r($query);



        }
    }

    public function confirm_reset_code()
    {
        $this->load->view('confirm_reset_code');
    }

    public function create_new_password()
    {
        $this->开发者_如何学JAVA;load->view('create_new_password');
    }
}

I enter email and nothing is returned to browser.


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.


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.

EDIT: Some changes for your new code:

  1. parent::Ci_Model(); should be parent::__construct(); (Don't mix php5 and php5 constructors)
  2. You need to call row() or row_array() on $query to get results. I suggest you don't return $query.
  3. $this->get_user_by_email->process(); should be $this->Get_user_by_email->process(); (Models start with an uppercase letter)


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


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消