簡體   English   中英

如何將多個值添加到數組的鍵並從另一個表獲取數據?

[英]How to add multiple values to a key of an array and get data from another table?

我正在嘗試從一個數據庫中獲取數據,條件是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'); 

正在做:

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

您只是覆蓋$my_array id密鑰。 用這個:

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

使用where field in ()條件中的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) . ')';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM