How can I recover data using a template and transmit it for use in a controller using CodeIgniter?


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'),
   'firstname' => //need to retrieve value from database,
   'lastname' => //need to retrieve value from database,
   'is_logged_in' => true


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


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.