我试图使用codeigniter框架实现高级搜索。我有一个搜索框,有几个图标作为搜索基础。然后,如果用户试图找到一个图标,例如停车场的图标,则它应该显示具有该图标的所有配置文件。如果用户点击更多图标,例如停车场、啤酒、情侣好。。。将只显示具有这些特征的配置文件。。
数据库信息:
-----------------------------------------------------
place_id icon_ids
-----------------------------------------------------
1 1,2,3,4,5,6,7,8
2 1,2,3,7,9
3 1,3,4
4 1,2,4,5,9,10
5 1,2,3,5,6,8,9,10
-----------------------------------------------------
型号
public function get_search_content($info_id, $search_queries, $query_type, $limit, $start, $action_type)
{
if($query_type === 'icons')
{
$count_id = explode(',', $info_id);
$count_id = count($count_id);
if($count_id > 1)
{
$search = "lai.info_id IN (".$info_id.")";
$query = $this->db->select('*, c.id AS cat_id, bh.id AS hour_id, bh.place_id AS bh_place_id, c.name AS cat_name, l.place_id AS place_id, lai.place_id AS lai_place_id')
->where($search)
->join('listing_additional_info lai', 'lai.place_id=l.place_id', 'LEFT')
->join('cities cy', 'cy.city_id=l.city_id', 'LEFT')
->join('cats c', 'c.parent_id=l.cat_id', 'LEFT')
->join('business_hours bh', 'bh.place_id=l.place_id', 'LEFT')
->group_by('lai.place_id', 'ASC')
->limit($limit, $start)
->get('listings l');
}
else
{
$search = "FIND_IN_SET('".$info_id."', lai.info_id)";
$query = $this->db->select('*, c.id AS cat_id, bh.id AS hour_id, bh.place_id AS bh_place_id, c.name AS cat_name, l.place_id AS place_id, lai.place_id AS lai_place_id')
->distinct()
->where($search)
->join('listing_additional_info lai', 'lai.place_id=l.place_id', 'LEFT')
->join('cities cy', 'cy.city_id=l.city_id', 'LEFT')
->join('cats c', 'c.parent_id=l.cat_id', 'LEFT')
->join('business_hours bh', 'bh.place_id=l.place_id', 'LEFT')
->group_by('lai.place_id', 'ASC')
->limit($limit, $start)
->get('listings l');
}
}
}
public function m_search_queries($limit, $start)
{
// $info_id = $this->input->get('info_id');
$info_id = $this->input->get('info_id_');
$search_queries = $this->input->get('q');
$submit_search = $this->input->get('submit_search');
if($submit_search == 'true')
{
if(!empty($info_id))
{
echo $this->get_search_content($info_id, $search_queries, 'icons', $limit, $start, 'content');
}
elseif(!empty($search_queries))
{
echo $this->get_search_content($info_id, $search_queries, 'input', $limit, $start, 'content');
}
else
{
$rs = '';
$rs .= '<div class="bg bg-info text-white p-3">Sorry, we could not find what you are looking for.</div>';
echo $rs;
}
}
}
查看
<form method="GET" action="<?= base_url().lang(); ?>/search" class="">
<div class="mb-2" id="add-data-info">
<?php for($i=1; $i<=13; $i++){ ?>
<div class="btn add_selector search_add_selector search_add_selector_<?= $i; ?> search_add_info_view">
<input type="checkbox" value="<?= $i; ?>" class="search_add_info">
<img id="" class="svg" src="<?= base_url(); ?>theme/myown/img/icon/svg/<?= $i; ?>.svg?<?= time(); ?>" width="28">
</div>
<?php } ?>
<div class="my-2">
<div class="w-100 border border-muted my-4"></div>
<input type="hidden" name="query_type" value="icons">
<button type="submit" name="submit_search" class="btn btn-info btn-lg btn-rounded" value="true">Search</button>
</div>
</div>
<input type="hidden" id="search_selected_additional" name="info_id_">
</form>
脚本
<script>
var _add_info = $('.search_add_selector .search_add_info').on('change', function() {
var add_info = $('.search_add_selector .search_add_info:checked');
var _selected = add_info.map(function() {return this.value;}).get().join(',')
var _checkIfChecked = $('.search_add_selector_'+this.value+' .search_add_info:checked').length > 0;
if(_checkIfChecked)
{
$('.search_add_selector_'+this.value).addClass('selected_info');
}
else
{
$('.search_add_selector_'+this.value).removeClass('selected_info');
}
$('#search_selected_additional').val(_selected);
});
</script>
例如:用户选择了1,2,5的值,然后只显示至少有1,2,5个图标id的地方。结果应该是:1 4 5
我使用的是find_in_set((函数,它在选中的1个图标上显示得很好。
但对于2个以上的图标,即使配置文件只有1个特征,它也会显示。我试图实现的是,当用户正在寻找一个有停车场、宠物店、啤酒的特定地方时,只有那些具有这些特征的地方才会显示出来。即使某些配置文件具有3个以上的特性。
只要配置文件至少具有这3个特性,就应该显示它。
我期待着你的帮助。非常感谢大家!
我创建了这个并在我的环境中进行了测试,它的工作原理就像一个魅力:
$this->db->select('whatever')->from('your_table');
$this->db->group_start();
foreach($selected_values as $value)
{
$this->db->where("find_in_set($value, icon_ids)");
}
$this->db->group_end();
$result = $this->db->get()->result_array();
我用你的值再次测试了它,它在(1,2,5(上运行良好,显示(1,4,5(