在 php 中实施全局搜索
Implement Global Search in php
我想像在购物车中那样实现搜索。
我有不止一个table。我使用下面给出的模型函数
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;
}
正常properly.But问题是
假设 body 名字是 xxx,颜色是黄色然后我搜索 xxx 它显示结果,我搜索 xxx 它显示结果。但是我在搜索框中输入 xxx yellow 它什么也没显示。我想列出这两个结果。
请给我一个解决方案...
您必须拆分查询。试试这个:
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;
}
}
我想像在购物车中那样实现搜索。 我有不止一个table。我使用下面给出的模型函数
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;
}
正常properly.But问题是 假设 body 名字是 xxx,颜色是黄色然后我搜索 xxx 它显示结果,我搜索 xxx 它显示结果。但是我在搜索框中输入 xxx yellow 它什么也没显示。我想列出这两个结果。 请给我一个解决方案...
您必须拆分查询。试试这个:
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;
}
}