使用Codeigniter Active Records中的like子句从单列进行多值过滤

时间:2018-04-09 08:58:16

标签: codeigniter activerecord mysqli sql-like

我尝试使用filter MySQL databaseCodeigniter Active Records列。我从下拉列表框中传递值并使用我想要过滤的数据。当我从不同的下拉列表中选择单个值时,我会得到适当的过滤结果。但是当我尝试使用单个下拉列表中的多个值进行过滤时,它会给出

  

找不到匹配的记录

data-table中的

。对于过滤我使用Like statement

以下是我的模型代码:

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)]);
        }
    }

我的问题是,如何将数组传递给like子句以过滤数据?

在上面我想选择一个多个集群来过滤数据。可以将AND operationStatement一样使用。

1 个答案:

答案 0 :(得分:1)

您可以使用 $ this-> db-> or_like()

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

参考:or_like()