2013-12-14 35 views
0

我試圖與其中id等於當前登錄的租戶(tenant_id)的條件從一個數據庫中獲取數據,如何將多個值添加到數組的某個鍵並從另一個表中獲取數據?

$this->data['props'] = $this->property_m->get_by(array ('id' => $this->session->userdata['tanant_id'])); 

,然後從現場得到一些值,並保存在一個數組,

if(count($this->data['props']) > 0){ 
    $my_array = array(); 
    foreach ($props as $prop){ 
     $my_array['id'] = $prop->tenant_id; 
    } 
} 

這裏的第一個問題 - $ my_array只包含1個值。 (我需要它是多個)我該怎麼做?
問題二 - 我需要從另一個表,該表將尋找數據滿足該數組中的條件,作爲選擇數據,

$this->data['reports'] = $this->report_m->get_by($my_array); 

它會說,

SELECT * FROM reports WHERE ('id = 1'); // or 2 or 3 (value from prop->tenant) 

,但我需要的是,

SELECT * FROM reports WHERE ('id = 1 OR id = 2 OR id = 3'); 
+1

1.不要覆蓋鍵'id'每次迭代。 2.'在哪裏IN()' –

+0

請告訴我如何不改寫密鑰ID –

回答

1

這樣做:

$my_array = array(); 
foreach ($props as $prop){ 
    $my_array['id'] = $prop->tenant_id; 
} 

你只是覆蓋id密鑰$my_array。使用此:

$my_array = array(); 
foreach ($props as $prop){ 
    $my_array[] = $prop->tenant_id; 
} 
// print_r($my_array); 

使用where field in()條件:

SELECT * FROM reports WHERE id IN (1, 2, 3);

假設你有:

$my_array = array(1,2,3); 
// query string: 
$q_str = 'SELECT * FROM reports WHERE id IN (' . implode(',', $my_array) . ')'; 
相關問題