使用Codeigniter活动记录中的like子句从单个列进行多值过滤
我正在尝试使用使用Codeigniter活动记录中的like子句从单个列进行多值过滤,codeigniter,activerecord,sql-like,Codeigniter,Activerecord,Sql Like,我正在尝试使用Codeigniter活动记录从MySQL数据库中过滤列。我正在从下拉列表框中传递值,并使用它来过滤数据。当我从不同的下拉列表中选择单个值时,我会得到正确的过滤结果。但当我尝试使用单个下拉列表中的多个值进行过滤时,它会给出 没有找到匹配的记录 在数据表中。对于过滤,我使用Like语句 这是我的型号代码: private function _get_datatables_query() { //add custom filter here /
Codeigniter
活动记录从MySQL数据库中过滤列。我正在从下拉列表框中传递值,并使用它来过滤数据。当我从不同的下拉列表中选择单个值时,我会得到正确的过滤结果。但当我尝试使用单个下拉列表中的多个值进行过滤时,它会给出
没有找到匹配的记录
在数据表中
。对于过滤,我使用Like语句
这是我的型号代码:
private function _get_datatables_query()
{
//add custom filter here
// if($this->input->post('startdate') && $this->input->post('enddate'))
// {
// $this->db->where("'FUPdate' BETWEEN 'startdate' AND 'enddate'");
// }
if($this->input->post('area'))
{
$this->db->like('area_name', $this->input->post('area'));
}
if($this->input->post('cluster'))
{
$clustersID['cluster']=$this->input->post('cluster');
if(!empty($clustersID['cluster'])){
// Array contains values, everything ok
$clusterString = implode(', ', $clustersID['cluster']);
}
if ($clusterString) {
$this->db->like('cluster_name', $clusterString);
} else {
$this->db->or_like('cluster_name', $clusterString);
}
}
if($this->input->post('timeframe'))
{
$this->db->like('timeframe_name', $this->input->post('timeframe'));
}
if($this->input->post('defect_status'))
{
$this->db->like('defect_status_name', $this->input->post('defect_status'));
}
if($this->input->post('startdate'))
{
$this->db->like('FUPdate', $this->input->post('startdate'));
}
if($this->input->post('enddate'))
{
$this->db->like('FUPdate', $this->input->post('enddate'));
}
$this->db->from($this->Property_Defect_View);
$i = 0;
foreach ($this->Property_Defect_search as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->Property_Defect_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($this->Property_Defect_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
我的问题是,如何传递array to like子句来过滤数据
在上面,我想选择一个多集群来过滤数据。是否可以将和操作与Like语句一起使用
您可以使用$this->db>或\u Like()
参考:即使我也这样尝试:如果($this->input->post('cluster')){$clustersID['cluster']=$this->input->post('cluster');如果(!empty($clustersID['cluster']){//数组包含值,一切正常$clusterString=内爆(',',',$clustersID['cluster']);}if($clusterString){$this->db->like('cluster\u name',$clusterString);}其他{$this->db->或类似('cluster\u name',$clusterString);},但没有输出。这是为了在群集列中查找多个群集。您应该输出查询并检查您真正想要的内容-没有人可以预测您真正想要的内容-执行查询后-只需放置一个echo$This->db->last\u query()
然后看一看-之后-用这个查询更新你的问题,定义你真正想要的-然后很容易帮助你-否则这只是一个猜测游戏。。。
$this->db->like('cluster_name', 'clusterString');
if($this->input->post('timeframe'))
{
$this->db->or_like('timeframe_name', $this->input->post('timeframe'));
}
if($this->input->post('defect_status'))
{
$this->db->or_like('defect_status_name', $this->input->post('defect_status'));
}
if($this->input->post('startdate'))
{
$this->db->or_like('FUPdate', $this->input->post('startdate'));
}
if($this->input->post('enddate'))
{
$this->db->or_like('FUPdate', $this->input->post('enddate'));
}