在php中实现全局搜索

时间:2015-01-13 12:07:11

标签: php mysql codeigniter

我想在购物车中实施搜索。 我有一张桌子。我使用下面给出的模型函数

function xenSelectCarsGlobal($key=null) 
    {
        $this->db->select('*');
        $this->db->from('sel_car_details');
        $this->db->join('derivative', 'derivative.der_id = sel_car_details.der_id', 'left');
        $this->db->join('variant', 'variant.var_id = sel_car_details.var_id', 'left');
        $this->db->join('model', 'model.model_id = sel_car_details.mod_id', 'left');
        $this->db->join('make', 'make.make_id = sel_car_details.mk_id', 'left');
        $this->db->join('car_color', 'car_color.color_id = sel_car_details.color_id', 'left');
        $this->db->join('body_type', 'body_type.body_id = sel_car_details.body_type_id', 'left');
        $this->db->join('fuel_type', 'fuel_type.fuel_id = sel_car_details.fuel_type_id', 'left');
        $this->db->join('gallery', 'gallery.sel_carid = sel_car_details.sel_car_id', 'left');
        $this->db->where('sel_car_details.status', '5');
        if(!empty($key))
            $this->db->or_like('fuel_type.fuel_name', $key);
        if(!empty($key))
            $this->db->or_like('sel_car_details.engine_size', $key);
        if(!empty($key))
            $this->db->or_like('sel_car_details.mileage', $key);
        if(!empty($key))
            $this->db->or_like('derivative.der_name', $key);
        if(!empty($key))
            $this->db->or_like('variant.variant', $key);
        if(!empty($key))
            $this->db->or_like('model.model', $key);
        if(!empty($key))
            $this->db->or_like('make.maker', $key);
        if(!empty($key))
            $this->db->or_like('car_color.col_name', $key);
        if(!empty($key))
            $this->db->or_like('body_type.body_name', $key);

        $qry = $this->db->get();

        if ($qry->num_rows() != 0) {
            return $qry->result_array();
        } else {
            return false;
        }return 1;
    } 

它正常工作。但问题是 假设身体名称是xxx,颜色是黄色然后我搜索xxx它显示结果,我搜索xxx它显示结果。但我在搜索框中输入xxx黄色它没有显示任何内容。我想列出结果。 请给我一个解决方案......

1 个答案:

答案 0 :(得分:1)

您必须拆分查询。试试这个:

function xenSelectCarsGlobal($strQuery=null) 
    {
        $arrKeywords = ($strQuery !== null) ? explode(' ', $strQuery) : array();

        $this->db->select('*');
        $this->db->from('sel_car_details');
        $this->db->join('derivative', 'derivative.der_id = sel_car_details.der_id', 'left');
        $this->db->join('variant', 'variant.var_id = sel_car_details.var_id', 'left');
        $this->db->join('model', 'model.model_id = sel_car_details.mod_id', 'left');
        $this->db->join('make', 'make.make_id = sel_car_details.mk_id', 'left');
        $this->db->join('car_color', 'car_color.color_id = sel_car_details.color_id', 'left');
        $this->db->join('body_type', 'body_type.body_id = sel_car_details.body_type_id', 'left');
        $this->db->join('fuel_type', 'fuel_type.fuel_id = sel_car_details.fuel_type_id', 'left');
        $this->db->join('gallery', 'gallery.sel_carid = sel_car_details.sel_car_id', 'left');
        $this->db->where('sel_car_details.status', '5');
        if(!empty($arrKeywords)) {
            foreach($arrKeywords as $strKeyword) {
                $this->db->or_like('fuel_type.fuel_name', $strKeyword);
                $this->db->or_like('sel_car_details.engine_size', $strKeyword);
                $this->db->or_like('sel_car_details.mileage', $strKeyword);
                $this->db->or_like('derivative.der_name', $strKeyword);
                $this->db->or_like('variant.variant', $strKeyword);
                $this->db->or_like('model.model', $strKeyword);
                $this->db->or_like('make.maker', $strKeyword);
                $this->db->or_like('car_color.col_name', $strKeyword);
                $this->db->or_like('body_type.body_name', $strKeyword);
            }
        }

        $qry = $this->db->get();

        if ($qry->num_rows() != 0) {
            return $qry->result_array();
        } else {
            return false;
        }
    }