RalphLeMouf Posted December 13, 2012 Share Posted December 13, 2012 I am trying to create my user module in codeigniter and can't figure out how to pass results from a query in my model to a controller that will allow me to use in my view. Here is my model: public function user() { $session = $this->session->userdata('is_logged_in'); $user_id = $this->session->userdata('id'); $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1"); foreach ($query->result() as $row) { $row->first_name; $row->id; $row->email; $row->last_name; } } and here is my controller. How to do I pass these objects? Or should I be putting them in an array and then passing the array ( still not sure how to pass it ) and then extracting them from the array on my controller? public function edit() { $data['first_name'] = $this->session->userdata('first_name'); $data['profile_icon'] = 'edit'; $data['profile_breadcrumbs'] = 'Edit Profile'; $data['main_content'] = 'account/edit'; $this->load->view('includes/templates/main_page_template', $data); $this->load->library('session'); $this->load->model('account_model'); $session = $this->session->userdata('session_id'); $this->account_model->user(); if($session) { //TRYING TO GET OBJECTS } } thanks in advance Link to comment https://forums.phpfreaks.com/topic/271968-how-do-i-pass-variables-from-a-model-to-a-controller-to-be-used-on-a-view-in-code-igniter/ Share on other sites More sharing options...
Jessica Posted December 13, 2012 Share Posted December 13, 2012 If in your model you fix your code to actually do something with "$row->first_name;" like maybe $this->first_name = $row->first_name; Then in your controller you can do: $this->account_model->user()->first_name; Link to comment https://forums.phpfreaks.com/topic/271968-how-do-i-pass-variables-from-a-model-to-a-controller-to-be-used-on-a-view-in-code-igniter/#findComment-1399226 Share on other sites More sharing options...
raknjak Posted December 21, 2012 Share Posted December 21, 2012 in your model: return $query->result(); in your controller: $result = $this->account_model->user(); $result is object Link to comment https://forums.phpfreaks.com/topic/271968-how-do-i-pass-variables-from-a-model-to-a-controller-to-be-used-on-a-view-in-code-igniter/#findComment-1400765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.