2017-05-31 6 views
0

Ich habe 3 Tabellen guest_user_info, pg_company, user_profile.Beitreten 3 Tabellen coderigniter php

In guest_user_info haben 2 Spalten:

g_uid | company_id 

In pg_company haben 2 Spalten:

company_id | user_id 

In user_profile haben 2 Spalten:

id |user_email 

Here i user_email erhalten möchten aus user_profile.i haben g_uid Wert (in guest_user_info Tabelle) wollen .i von guest_user_info company_id und die company_id zu erhalten und mit pg_company Tabelle übereinstimmen, da kann ich user_id.then Spiel bekommen mit diesem user_id mit id in user_profile table.at letzten ich brauche user_email aus user_profile Tabelle

Antwort

0

seine Well ein einfacher, Sie müssen nur mit aktiver Abfrage in CodeIgniter beitreten.

$this->db->select("UP.id", "UP.user_email"); 
$this->db->from("guest_user_info AS GU"); 
$this->db->join("pg_company AS PC", "PC.company_id=GU.company_id"); 
$this->db->join("user_profile AS UP", "UP.id=PC.user_id"); 
$this->db->where("GU.g_uid", $guid); 
$query = $this->db->get(); 

return $query->result(); 

In obigen Code $guid Sie haben zu schaffen, die Sie haben.

auch einen Blick auf diese Links nehmen Sie bitte:

https://www.codeigniter.com/userguide3/database/query_builder.html

https://www.codeigniter.com/userguide2/database/active_record.html

Sie erhalten so viele Dinge, nach der Lektüre dieses.

+0

Vielen Dank.Es funktioniert gut –

+0

upvote, wenn Sie die richtige Antwort erhalten haben. – kishor10d

0
Check bellow code it`s working fine and common model function also 
    supported more then one join and also supported multiple where condition 
order by ,limit.it`s EASY TO USE and REMOVE CODE REDUNDANCY. 

    ================================================================ 
    *Album.php 
    //put bellow code in your controller 
    ================================================================= 

    $album_id='';//album id 

    //pass join table value in bellow format 
    $join_str[0]['table'] = 'pg_company'; 
    $join_str[0]['join_table_id'] = 'pg_company.company_id'; 
    $join_str[0]['from_table_id'] = 'guest_user_info.company_id'; 
    $join_str[0]['join_type'] = '';//set join type 

    $join_str[1]['table'] = 'user_profile'; 
    $join_str[1]['join_table_id'] = 'user_profile.id'; 
    $join_str[1]['from_table_id'] = 'guest_user_info.user_id'; 
    $join_str[1]['join_type'] = ''; 


    $selected ="guest_user_info.*,user_profile.user_name,pg_company.name"; 
    $condition_array=array('guest_user_info.g_uid' => $g_uid);  
    $albumData= $this->common->select_data_by_condition('guest_user_info', $condition _array, $selected, '', '', '', '', $join_str); 
           //call common model function 

    if (!empty($albumData)) { 
     print_r($albumData); // print album data 
    } 

========================================================================= 
    Common.php 
    //put bellow code in your common model file 
======================================================================== 

    function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) { 
    $this->db->select($data); 
    //if join_str array is not empty then implement the join query 
    if (!empty($join_str)) { 
     foreach ($join_str as $join) { 
      if ($join['join_type'] == '') { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']); 
      } else { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']); 
      } 
     } 
    } 

    //condition array pass to where condition 
    $this->db->where($condition_array); 


    //Setting Limit for Paging 
    if ($limit != '' && $offset == 0) { 
     $this->db->limit($limit); 
    } else if ($limit != '' && $offset != 0) { 
     $this->db->limit($limit, $offset); 
    } 
    //order by query 
    if ($sortby != '' && $orderby != '') { 
     $this->db->order_by($sortby, $orderby); 
    } 

    $query = $this->db->get($tablename); 
    //if limit is empty then returns total count 
    if ($limit == '') { 
     $query->num_rows(); 
    } 
    //if limit is not empty then return result array 
    return $query->result_array(); 
}