Хотите создать окно поиска с автозаполнением, используя codeigniter - PullRequest
0 голосов
/ 21 октября 2019
    I want to create a search box using the Codeigniter framework with auto-fill options. The search box is working properly but the issue is, it is not giving any options to the user.
    I have not tried any option yet, as I am new to the framework.

    **Controller:**

         public function search($subcategory=''){
        $this->load->helper(array('form', 'url','common'));
        $this->load->library(array('session','pagination'));
        $this->load->model('homemodel','',TRUE);
        if(isset($_GET['term'])){
            $results= $this->homemodel->search($_GET['term'],$_GET);
            if(count($results) > 0)
            {
                foreach($results as $result)
                $arr_result[] = $result->product_name;
                echo json_encode($arr_result);
            }
        }


        $data['category']=$this->homemodel->getAllCategories();
        $data['subcategory']=$subcategory;
        $data['mens']=$this->homemodel->getAllMen();
        $data['womens']=$this->homemodel->getAllWomen();
        $data['scarves']=$this->homemodel->getAllScarve();
        $data['collections']=$this->homemodel->getAllCollection();

        $data['material']=['Cotton'=>'Cotton','Polyster'=>'Polyster'];
        $data['color']=['Red'=>'Red','Black'=>'Black','Blue'=>'Blue','Green'=>'Green','Orange'=>'Orange','Black'=>'Black'];
        $data['size'] =['S'=>'S','M'=>'M','L'=>'L','XL'=>'XL','XXL'=>'XXL','XXL'=>'XXL'];
        $data['subcat']=$this->homemodel->getAllSubCat($_GET['term'],$_GET);
        $data['price']=$this->homemodel->getPrice($_GET['term'],$_GET);

        $this->load->view('products',$data);
    }

    **Model:**

        function search($key,$search=array(),$cat_array=array()){

                $search=array_filter($search);

                $this->db->select('product.*');
                $this->db->select('product_images.features');
                $this->db->select('product_images.image');
                $this->db->from('product');
                $this->db->join('product_images',"product.id=product_images.product_id and product_images.features='yes'");
                $this->db->join('product_material',"product.id=product_material.product_id",'left');
                $this->db->join('product_color',"product.id=product_color.product_id",'left');
                $this->db->join('product_size',"product.id=product_size.product_id",'left');

                if(isset($cat_array->sub_cat_id) && $cat_array->sub_cat_id!=''){
                    $this->db->where('product.product_subcategory',$cat_array->sub_cat_id);
                }
                if(isset($search['material']) && $search['material']!=''){
                    $this->db->where_in('product_material.material',explode(",",$search['material']));
                }
                if(isset($search['color']) && $search['color']!=''){
                    $this->db->where_in('product_color.color',explode(",",$search['color']));
                }
                if(isset($search['size']) && $search['size']!=''){
                    $this->db->where_in('product_size.size',explode(",",$search['size']));
                }
                if(isset($search['price']) && $search['price']!=''){
                    $this->db->where_in('product.product_price',explode(",",$search['price']));
                }   

$this->db->like('product.product_name',$key);
$this->db->where("product.status='active'");
$this->db->group_by(['product_color.product_id','product_material.product_id','product_size.product_id']);
                $query=$this->db->get(); 
                $result=$query->result();
                return $result;
            }
         function search($name,$search=array(),$cat_array=array()){

            $search=array_filter($search);

            $this->db->select('product.*');
            $this->db->select('product_images.features');
            $this->db->select('product_images.image');
            $this->db->from('product');
            $this->db->join('product_images',"product.id=product_images.product_id and product_images.features='yes'");
            $this->db->join('product_material',"product.id=product_material.product_id",'left');
            $this->db->join('product_color',"product.id=product_color.product_id",'left');
            $this->db->join('product_size',"product.id=product_size.product_id",'left');

            if(isset($cat_array->sub_cat_id) && $cat_array->sub_cat_id!=''){
                $this->db->where('product.product_subcategory',$cat_array->sub_cat_id);
            }
            if(isset($search['material']) && $search['material']!=''){
                $this->db->where_in('product_material.material',explode(",",$search['material']));
            }
            if(isset($search['color']) && $search['color']!=''){
                $this->db->where_in('product_color.color',explode(",",$search['color']));
            }
            if(isset($search['size']) && $search['size']!=''){
                $this->db->where_in('product_size.size',explode(",",$search['size']));
            }
            if(isset($search['price']) && $search['price']!=''){
                $this->db->where_in('product.product_price',explode(",",$search['price']));
            }   

            $this->db->like('product.product_name',$name,'both');
            $this->db->where("product.status='active'");
            $this->db->group_by(['product_color.product_id','product_material.product_id','product_size.product_id']);
            $query=$this->db->get(); 
            $result=$query->result();
            return $result;
        }

    **View:**

        <form action="<?php echo base_url();?>home/search" method="POST">
         <input placeholder="search" name="title" required=""  id="search" type="text" >
      <button type="submit" id="search_button" ><i class="fas fa-search"></i></button>
    </form>
    <script>
             $(function() {
                $( "#search" ).autocomplete({
                   source: "<?php echo base_url();?>home/search/?",
                   autoFocus:true
                });
             });
    </script>

Как эта функциональность может быть создана с использованием Codeigniter и MySQL, пожалуйста, помогите с этим, любая ссылка также будет оценена, если вы можете дать. Я пробовал использовать функцию автозаполнения jquery, но не смог сделать это. Должен ли я попробовать typehead.js или hounds.js. Я не уверен в этом. Как эта функциональность может быть создана с использованием Codeigniter и MySQL, пожалуйста, помогите с этим, любая ссылка также будет оценена, если вы можете дать. Я пытался использоватьФункция автозаполнения jquery, но не смогла это сделать. Должен ли я попробовать typehead.js или hounds.js. Я не уверен в этом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...