在codeigniter中创建具有多列的搜索栏

问题描述 投票:0回答:1
Controller:
public function search_products_auto(){
        $this->load->helper(array('form', 'url','common'));
        $this->load->library(array('session','pagination'));
        $this->load->model('homemodel','',TRUE);
        $this->homemodel->search_products_auto();
    }

 public function search($subcategory=''){
        $this->load->helper(array('form', 'url','common'));
        $this->load->library(array('session','pagination'));
        $this->load->model('homemodel','',TRUE);
        $search_name=$this->input->post("title"); // first get search character

        $data['records']=$this->homemodel->search($search_name,$_GET); // SearchModel is the model class name


        $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($search_name,$_GET);
        $data['price']=$this->homemodel->getPrice($search_name,$_GET);

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


Model:
 function search($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']));
        }   
        if(isset($search['search']) && $search['search']!=''){
            $this->db->or_like(array('product.product_name'=>$search['search'],'product.sku_number'=>$search['search']));
        }   


        $this->db->or_like(array('product.product_name'=>$search_name,'product.sku_number'=>$search_name));
        $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_products_auto(){
            $search_item = $this->input->post('query');

            $this->db->select(['product.id','product.product_name','product.sku_number']);
            $this->db->from('product');
            if($search_item!=''){
                $this->db->or_like(array('product.product_name'=>$search_item,'product.sku_number'=>$search_item));

            }

            $this->db->where("product.status='active'");
            $query=$this->db->get(); 
            $result=$query->result();
            $dataSearch=[];
            if(count($result)>0){
                $i=0;
                foreach($result as $results)

                {
                    $dataSearch[$i]['id']=isset($results->id)?$results->id:"";
                    $dataSearch[$i]['name']=isset($results->product_name)?$results->product_name:""; 
                    $dataSearch[$i]['name']=isset($results->sku_number)?$results->sku_number:"";

 $i++;   
                }

            }
            echo json_encode($dataSearch);

        }  

现在,我在搜索栏中仅得到sku数字值,而没有其他值。如何在一个搜索栏中获得全部三个。我无法理解将3集成到一个数组中的概念,除此搜索栏之外,其他所有东西都工作正常,搜索结果也不理想。

search codeigniter-3
1个回答
0
投票

请尝试这些代码,它可能对您有帮助,我仅在Model中有更改:

function search($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'");

        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['price']) && $search['price']!=''){
            $this->db->where_in('product.product_price',explode(",", $search['price']));
        } 

        if(isset($search['search']) && $search['search']!=''){
            $this->db->or_like(array('product.product_name' => $search['search'],'product.sku_number' => $search['search']));
        } 

        if(isset($search['material']) && $search['material']!=''){
            $this->db->join('product_material',"product.id=product_material.product_id",'left');
            $this->db->where_in('product_material.material',explode(",",$search['material']));
        }

        if(isset($search['color']) && $search['color']!=''){
            $this->db->join('product_color',"product.id=product_color.product_id",'left');
            $this->db->where_in('product_color.color',explode(",",$search['color']));
        }

        if(isset($search['size']) && $search['size']!=''){
            $this->db->join('product_size',"product.id=product_size.product_id",'left');
            $this->db->where_in('product_size.size',explode(",",$search['size']));
        }

        $this->db->or_like( array('product.product_name' => $search_name, 'product.sku_number' => $search_name));
        $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_array = $query->result_array();
        /* $result=$query->result(); */
        return $result_array;
    }

    function search_products_auto(){

            $search_item = $this->input->post('query');
            $this->db->select(['product.id','product.product_name','product.sku_number']);
            $this->db->from('product');
            if($search_item != ''){
                $this->db->or_like(array('product.product_name' => $search_item, 'product.sku_number' => $search_item));
            }
            $this->db->where("product.status","active");
            $query = $this->db->get(); 
            /* $result=$query->result();
            $dataSearch=[];
            if(count($result)>0){
                $i=0;
                foreach($result as $results)

                {
                    $dataSearch[$i]['id']=isset($results->id)?$results->id:"";
                    $dataSearch[$i]['name']=isset($results->product_name)?$results->product_name:""; 
                    $dataSearch[$i]['name']=isset($results->sku_number)?$results->sku_number:"";

 $i++;   
                }

            }
            echo json_encode($dataSearch); */

            echo json_encode($query->result_array());
        }  
© www.soinside.com 2019 - 2024. All rights reserved.