[英]How to use where_in using Active Record Codeigniter?
I want to pass array to where_in using Active Record. 我想使用Active Record将数组传递给where_in。
I saw few reference link, can pass like this: 我看到很少的参考链接,可以这样通过:
$this->db->where_in('id', array('20','15','22','42','86'));
Query : 查询:
$mts = $this->db->select('subcat_id')->get_where('subcategory_tbl', array('cat_id' => $cat_id));
$result = $mts->result_array();
But My result Array in this form : 但是我的结果数组以这种形式:
Array
(
[0] => Array
(
[subcat_id] => 12
)
[1] => Array
(
[subcat_id] => 13
)
)
So how can i pass this array id to where_in clause ? 那么如何将这个数组ID传递给where_in子句呢? I am confused here. 我在这里很困惑。
You can do it like: 你可以这样做:
$this->db->where_in('cat_id', $cat_id)
$this->db->select('subcat_id');
$this->db->get('subcategory_tbl');
assuming that $cat_id
contains the array of values to check. 假设$cat_id
包含要检查的值的数组。
You need to convert your result (array of rows) to an array of the values in the first "column" of those rows: 您需要将结果(行数组)转换为这些行的第一个“列”中的值的数组:
$ids = array_map(function($row) { return $row['subcat_id']; }, $result);
You probably should check $ids isn't empty before using it - since (I can't remember but I think) where_in will happily generate the invalid 您可能应该在使用$ ids之前先检查它是否为空-因为(我不记得了,但我想)where_in会很高兴地生成无效的
SELECT ... WHERE id IN ()
if you pass it an empty array. 如果您将其传递给一个空数组。
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
Use your first query like 使用您的第一个查询,例如
$mts = $this->db->select('GROUP_CONCAT(subcat_id) as new')->get_where('subcategory_tbl', array('cat_id' => $cat_id))->row()->new;
and then use 然后使用
if($mts != "")
{
$this->db->where_in('id', $mts);
}
Hope it helps you.... 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.