Php 如何在何处条件下传递多个Id数组?

Php 如何在何处条件下传递多个Id数组?,php,codeigniter,Php,Codeigniter,我想在何处条件下传递多个id怎么做 查询从数据库中获取org\u id 现在我想在我的where条件下通过,那么怎么做呢 我尝试了foreach循环: 下面是我的代码: $devices = $this->activation_m->get_activated_devices(); $org_id = array(); // create org_id array foreach ($devices as $row) {

我想在何处条件下传递多个id怎么做

查询从数据库中获取
org\u id

现在我想在我的where条件下通过,那么怎么做呢

我尝试了foreach循环:

下面是我的代码

$devices =  $this->activation_m->get_activated_devices();


    $org_id = array(); // create org_id array
        foreach ($devices as $row)
        {

         $org_id[]= $row['org_id']; // assign ids into array

            //$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
        }

     //Now $org_id is array


    $companys =$this->data['devices'] = $this->activation_m->get_company($org_id);  
    echo $this->db->last_query();
型号代码

    public function get_activated_devices()
    {
        $this->db->select('*');
        $this->db->join('sitekey','sitekey.site_key = activation.site_key');
        $this->db->from('activation');
        $query =$this->db->get();
        $result = $query->result_array(); 
        return $result; 




    }

    public function get_company($org_id)
    {

        $this->db->select('*');
        $this->db->join('sitekey','sitekey.org_id = company.id');
        $this->db->join('activation','sitekey.site_key = activation.site_key');
        $this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
        $this->db->from('company');
        $query =$this->db->get();
        $result = $query->result_array(); 
        return $result; 
    }

现在我的查询只通过了一个
org\u id
,我想通过我第一次查询得到的所有
org\u id

你可以使用codeigniter的
$this->db->或_where()
达到这个目的。只需遍历组织id的数组并应用或_where条件

    $this->db->select('*');
    $this->db->join('sitekey','sitekey.org_id = company.id');
    $this->db->join('activation','sitekey.site_key = activation.site_key');
    foreach($org_id as $org)
    {    // where $org is the instance of one object of active record
         $this->db->or_where('company.id',$org);
    }
    $this->db->from('company');
    $query =$this->db->get();
    $result = $query->result_array(); 
    return $result; 

另一种方法是通过像这样遍历数组并在字符串中附加where子句来进行自定义查询。

您可以使用active records codeigniter中的
where\u

作为


嘿,这是可行的,但显示错误:未定义变量:检查_ids@Rajan编辑立即检查:)当我的表中没有任何记录时,此操作失败。我显示语法错误。请立即给出该检查的解决方案@Rajant这个条件应该在我看来?@PraveenKumar我得到错误未定义变量:check_id
$devices =  $this->activation_m->get_activated_devices();
$org_id = array();  
foreach ($devices as $row)
{    
 $org_id[] = $row['org_id'];           
}
 $companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
   echo "<pre>";
   print_r( $companys->result());
   echo "</pre>";
}
public function get_company($org_ids = array())
{
    $this->db->select('*');
    $this->db->join('sitekey','sitekey.org_id = company.id');
    $this->db->join('activation','sitekey.site_key = activation.site_key');
    $this->db->where_in('company.id', $org_ids );  //this is condition       
    $this->db->from('company');
    return $this->db->get();
}