如何检查像Codeigniter这样的查询会返回多个like中的数据

时间:2018-10-29 04:15:47

标签: php codeigniter

我试图找出如何检查类似查询返回数据的方法。用户可以搜索为1.位置2.供应商名称,3.类别名称。假设用户搜索印度,印度也可以作为供应商名称。现在查询返回一些结果作为位置,而另一些返回作为供应商名称。我想将位置匹配结果存储到位置数组中, 供应商名称匹配结果进入vendor_name数组,然后返回两个数组。

这是我的模特

<?php 
    class Search_M extends CI_Model{
        public function getSearchResults($search_query){
            $this->db->select('ov_activity.activity_id,ov_activity.timing,vendor_name,vendor_img,address');
            $this->db->from('ov_activity');
            $this->db->join('ov_vendors', 'ov_activity.vendor_id = ov_vendors.vendor_id','left');
            $this->db->join('ov_category', 'ov_activity.category_id = ov_category.category_id','left');
            $this->db->like('ov_vendors.vendor_name', $search_query);
            $this->db->or_like('ov_vendors.address', $search_query);
            $this->db->or_like('category_name', $search_query);

            $query  =   $this->db->get();
            return $query->result();
        }
    }
?>

这是我的控制人

<?php
    public function search_post() {
        $is_valid_token  = $this->authorization_token->validateToken();

        if(!empty($is_valid_token) AND $is_valid_token['status'] === TRUE)
        {
            $searchData = array();
            $searchData['query'] = $this->post('query');

            $getSearchResults = $this->Search_M->getSearchResults($searchData['query']);
            echo "<pre>";
            print_r($getSearchResults);
            exit;
    }
?>

1 个答案:

答案 0 :(得分:1)

尝试以下逻辑,

  function filter_record($result,$searchKeyword) {
     $setArr = array(); 
     $setArr['Location'] =  array();
     $setArr['Cat'] = array(); 
     $setArr['Vendor'] = array(); 

    foreach($result as $row) {
        if (stristr($row->location_field, $searchKeyword) ) {
                $setArr['Location'][] = $row;
        }else if (stristr($row->cat_field, $searchKeyword) ) {
                $setArr['Cat'][] = $row;
        }else if (stristr($row->vendor_field, $searchKeyword) ) {
                $setArr['Vendor'][] = $row;
        }


    }
    return   $setArr;
}