简体   繁体   English

Codeigniter如何连接3表并从用户ID显示

[英]codeigniter how to join 3 table and display from user id

Mydatabase image click to see Mydatabase图片点击查看

i want to display 我想展示

group1 组1

  • shop1 shop1
  • shop2 shop2
  • shop3 shop3

group2 组2

  • shop1 shop1
  • shop2 shop2
  • shop3 shop3
  • shop5 shop5

group2 组2

  • ..... .....
  • .... ....
  • .... ....

function getGroups($user_id) {

    $this->db->select ( '*' ); 
    $this->db->from ( 'user' );
    $this->db->join ( 'groups', 'groups.user_id = user.id');
    $this->db->join ( 'shop', 'shop.group_id = groups.group_id');
    $this->db->where('user.id',$user_id);
    return $this->db->get()->result_array();
}

Thank you all for Answer. 谢谢大家的回答。 I may not be don't understand for the above Answer. 对于以上答案,我可能不是不明白。 I use this method instead. 我改用这种方法。 It may not be the correct way. 这可能不是正确的方法。 work for me. 为我工作。

function getGroups($user_id) {
    $this->db->select('*');
    $this->db->from('groups');
    $this->db->where('user_id', $user_id);
    $this->db->order_by('group_id', 'ASC');
    $group_data = $this->db->get()->result_array();

    foreach($group_data as $key=>$value){

        $this->db->select('*');
        $this->db->from('shop');
        $this->db->where('group_id', $value['group_id']);
        $this->db->order_by('shop_id', 'ASC');
        $shop_data = $this->db->get()->result_array();

        $my_group[$key]['group_name'] = $value['group_name'];
        $my_group[$key]['group_id'] = $value['group_id'];
        $my_group[$key]['group_shop'] = $shop_data;


    }
    return $my_group;
}

Add group by groups.id,shop.id and order by groups.id . 添加group by groups.id,shop.id and order by groups.id

function getGroups($user_id) {
    $this->db->select ( '*' ); 
    $this->db->from ( 'user' );
    $this->db->join ( 'groups', 'groups.user_id = user.id');
    $this->db->join ( 'shop', 'shop.group_id = groups.group_id');
    $this->db->where('user.id',$user_id);
    $this->db->group_by(array('groups.id','shop.id'));
    $this->db->order_by("groups.id", "desc");
    return $this->db->get()->result_array();
}

try this.. 尝试这个..

function getGroups($user_id)
 {
    $this->db->select ( '*' ); 
    $this->db->from ( 'user' );
    $this->db->join ( 'groups', 'groups.user_id = user.user_id');
    $this->db->join ( 'shop', 'shop.group_id = groups.group_id');
    $this->db->where('user.user_id',$user_id);
    $this->db->group_by(array('groups.id','shop.id'));
    $this->db->order_by("groups.id", "desc");
    return $this->db->get()->result_array();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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