gpt4 book ai didi

php - 如何在where条件codeigniter中传递多个Id数组?

转载 作者:可可西里 更新时间:2023-11-01 00:58:09 25 4
gpt4 key购买 nike

我想在where条件下传递多个id怎么办?

查询从数据库中获取 org_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_id ,我想传递我从第一个查询中获得的所有 org_id

最佳答案

您可以使用来自 active-records codeigniter 的 where_in

作为

$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();
}

关于php - 如何在where条件codeigniter中传递多个Id数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35355458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com