Welcome Guest, Not a member yet? Register   Sign In
returning data array from model to controller
#1

[eluser]netrox[/eluser]
Just exactly how do I retrieve data as array and then assigning it to $data[var]?

For example I want to get an array of two fields such as userID and username from a table and I create a function in model class:

Code:
function getUser($email, $password)
        {
        $data=array();
        $sql="SELECT * FROM users";
        $query=$this->db->query($sql);
        $row=$query->row();
        $data['firstname']=$row['firstname'];
        $data['userID']=$row['userID'];
        return $data;
        }

But the thing is I don't understand how I can get data array to be passed to controller from model. I wrote like this but it doesn't seem right:

Code:
$data['user']=$this->Musers->getUser($email, $pswd);

It doesn't seem right because I need to know which var is userID and firstname. Doing $this->session->set_userdata('userID', $data['userID']) does not seem right to me either. How do I exactly assign the data array to a returned $data from model to controller?
#2

[eluser]InsiteFX[/eluser]
Try this:

Code:
function getUser()
{
    $data = array();

    $query = $this->db->get('users');

    if ($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            $data[] = $row;
        }
    }

    $query->free_result();
    return $data;
}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB