1

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

Akshay Deshmukh
  • 1,242
  • 1
  • 16
  • 31
  • What's your question? Are you stuck at a point and don't know how to implement a specific logic you have thought of? Do you want us to suggest an autocomplete formula or to write some autocomplete code for you or to implement your code into the rest of your app? Or does your code have errors? Or does it work and you want us to look at it? In any case: your question either does not provide enough details (e.g. an error) or is not suitable on stack overflow (e.g: write the code for you). One suggestion though: use prepared statements. Try e.g. searching for a product that contains `'`. – Solarflare Aug 10 '17 at 11:11
  • @Solarflare, sorry for inconvenience. I dont want any one to write code for me and I just mentioned my code whatever i have done till now. I just want to know what is the best way for autocomplete in ecommerce web app as user can enter any thing as search string. I just want someone to told me the correct logic – Akshay Deshmukh Aug 10 '17 at 12:47
  • There is not THE correct logic (although I would expect an autocomplete to find words that begin with the (last partial word of your) input, not contain them anywhere; it looks more like (the searching part of) a (maybe live) search right now, displaying all possible products that currently match the input, but not really autocompleting the word you are currently starting to write). If your search works and you get the results you are looking for, it is fine, if not, well, we don't know what logic you are looking for, but we cannot tell you what you are looking for either. – Solarflare Aug 10 '17 at 13:12

0 Answers0