Welcome Guest, Not a member yet? Register   Sign In
Help, Passing data from Model to Controller
#1

[eluser]Unknown[/eluser]
I am trying to pass over the ID field of a query to pass into session data in my Controller.

I can echo the ID out successfully, but I have no idea how to move that data into my controller.


Code:
function validate(){
  $this->db->where('username', $this->input->post('user'));
  $this->db->where('password', sha1($this->input->post('pass')));
  $query = $this->db->get('users');

  if($query->num_rows == 1){
   foreach($query->result() as $row):
    $userID = $row->id;//echoing this works
   endforeach;

   return $userID;
  
  }
}
When I try to use $userID in my controller, it throws an error of undefined variable. How do I get this variable into my controller?

Sorry if this is a nooby question, I just started learning codeigniter and I am porting one of my current websites from regular php to codeingiter.
#2

[eluser]InsiteFX[/eluser]
For one you are getting an object not an array.
Code:
public function validate()
{
    $this->db->where('username', $this->input->post('user'));
    $this->db->where('password', sha1($this->input->post('pass')));
    $query = $this->db->get('users');

    if($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            return $row['id'];
        }
    }

    return FALSE;
}

// in your controller
class Name extends CI_Controller {

    private $user_id = NULL;

    public function index()
    {
        $user_id = $this->your_model_name->validate();
        echo $user_id;
    }
}
#3

[eluser]Unknown[/eluser]
Thanks, that got it working.




Theme © iAndrew 2016 - Forum software by © MyBB