开发者

How can I retrieve data using a model and pass it to be used in a controller using CodeIgniter?

开发者 https://www.devze.com 2023-03-10 09:57 出处:网络
From a user login form i need to use their username ($this->input->post(\'username\')) to query against the membership table in my database so that I can retrieve the users firstname and lastnam

From a user login form i need to use their username ($this->input->post('username')) to query against the membership table in my database so that I can retrieve the users firstname and lastname, which I believe should be done via a model called from my controller. I then need to pass that data back to my controller to use the firstname and lastname to add in to my session userdata which is set in my controller, as shown below.

$data = array(
   'username' => $this->input->post('username'),
开发者_Python百科   'firstname' => //need to retrieve value from database,
   'lastname' => //need to retrieve value from database,
   'is_logged_in' => true
);

$this->session->set_userdata($data);

Thanks, Kristan.


In your model, make a function to retrieve the first and last name of a user:

 public function get_user_details($username){
     // retrieve contents from db:
     // the first username here is the name of the column in your table

     $query = $this->db->select('firstname, lastname')
                       ->where('username', $username)
                       ->get('membership');
     return $query->result_array();
 }

In your controller, like you were doijng, grab the POST contents in your controller:

 $username = $this->input->post('username');

Then, from your controller, call your model function but save the output of that model function in a variable:

 $data = $this->user_model->get_user_details($username);

After that, you can do what you were trying to do:

 $this->session->set_userdata($data);

Hope that helps. I wrote this from the top of my head but it should work. I also suggest you read the CI documentation through because it really is some of the best documentation I have ever seen for a framework. Good luck.


Please check CodeIgniter user guide for model. The example of Blog controller and Blog model will answer you.

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

class Blog_controller extends CI_Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}


You first need to create a model.

class Membership_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function getUser($username) {
        $query = $this->db->get_where('membership', array('username' => $username));
        return $query->result_array();
    }
}

then in your controller, use the membership model to retrieve data. Calling this function: $this->membership_model->getUser($this->input->post('username'));

0

精彩评论

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

关注公众号