Jump to content

Archived

This topic is now archived and is closed to further replies.

RalphLeMouf

How Do I Pass Variables From A Model To A Controller To Be Used On A View In Code Igniter?

Recommended Posts

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

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

in your model:

 

return $query->result();

 

in your controller:

 

$result = $this->account_model->user();

 

$result is object

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.