I am working on one eCommerce web app where i want to give autocomplete suggestions. I have categories, sub categories, sub sub categories and product title. So which logic should be apply here.
Following is my code currently i am using
if (strlen($productString) > 0) {
$arrSearchString = explode(' ', $productString);
$noSpaceSearchString = preg_replace('/\s+/', '', $productString);
$whereConditionString = "";
foreach ($arrSearchString as $string) {
if (strlen($string) > 0)
$whereConditionString .= "category_name like '%$string%' or ";
}
$whereConditionString .= "category_name like '%$noSpaceSearchString%' or category_name like '%$productString%' ";
$this->db->select("DISTINCT(category_id)");
$this->db->from('tbl_category');
$this->db->where("($whereConditionString)");
$this->db->where(array('is_deleted' => 0));
$categoryIdQuery = $this->db->get_compiled_select();
$this->db->flush_cache();
}
$this->db->select('distinct(p.product_title), c.category_name');
$this->db->from('tbl_product p');
$this->db->join('tbl_category c', 'p.product_category_id = c.category_id');
$this->db->where("(p.product_category_id IN ($categoryIdQuery) or p.product_subcategory_id IN ($categoryIdQuery) or p.product_sub_subcategory_id IN ($categoryIdQuery) or p.product_title like '%$productString%' or p.search_tag like '%$productString%' )", NULL, FALSE);
$this->db->where(array(
'p.product_status' => 1,
'p.is_deleted' => 0,
'c.is_deleted' => 0,
));
$this->db->order_by('p.product_title', 'asc');
$result = $this->db->get();
return $result->result();
Here consider $productString as search string. I have checked with the each word in string as well as string with no space.
Thanks in advance