简体   繁体   中英

returning array from Model to Controller codeigniter

I'm trying to query two values from database, with 2 different IDs and I need to get all the results on one array. This is part of my controller.

function account()
{
    $user_id = $this->session->userdata('user_id');
    $this->load->model("Site_model");
    $q = $this->Site_model->get_all_notify($user_id);
}

and here is the Model:

function get_user_data_by_id($id) {
    $this->db->where("id",$id);
    $q=$this->db->get("users");
    return  $q->result();
}

function get_all_notify($user_id) {
    $this->db->where("wanted_id",$user_id);
    $this->db->where("requests_id",1);
    $q=$this->db->get("intrested");
    foreach($q->result() as $row) {
        $user=$this->get_user_data_by_id($row->users_id);
    }
    return $user;
}

But when I print_r $q from controller I only get 1 row... but when I print_r $user I get all the results! Even with foreach nothing seems to work!

You need to make array and store the users data in that,in current code the last iteration data from foreach will be return

$user=array();
foreach($q->result() as $row)
{
        $user[]=$this->get_user_data_by_id($row->users_id);
}
return $user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM