使用codeigniter的多个过滤器,我只需要批准的产品?

问题描述 投票:0回答:1

我正在使用codeigniter进行多次搜索,但是所有产品均从数据库中显示。我只需要批准的产品。

当我尝试在产品视图页面中显示时,所有产品均从产品表中显示。

控制器:

public function product()
    {
       $searchText="";
       if($this->input->post('searchText'))
        {
          $searchText = $this->input->post('searchText');
        }
        $like = array(
            'ProductName'=>$searchText
        );
        $orlike = array(
            'CategoryName'=>$searchText,
            'SubCategoryName'=>$searchText,
            'BrandName'=>$searchText
        );
        $where = array{
             'ProductIsActive'=>1,
             'ProductStatus'=>'Approved'
        );
        $jointables = array(
            'categories'=>'CategorySlug=ProductCategory',
            'subcategories'=>'SubCategorySlug=ProductSubCategory',
            'brands'=>'BrandSlug=ProductBrand'
         );

         $data['product']= $this-> 
Custom_model-> getproduct('products',$jointables,$where,$like,$orlike,array()); 
    }

型号:

  function getproduct($primarytable,$jointables,$where,$like,$orlike)
    {
        $this->db->select('*');
        $this->db->from($primarytable);
        $this->db->where($where);
        $this->db->like($like);
        foreach($orlike as $key=>$value)
        {
            if($value!="")
            {
                $this->db->or_like($key,$value,'both');
            }           
        }
        foreach($jointables as $key=>$value)
        {
            $this->db->join($key,$value,'left');
        }       

         $query = $this->db->get();
        if($query->num_rows() > 0)
        {
                $row = $query->result();
                return $row;
        }else{
                return FALSE;
        }
    }
php codeigniter
1个回答
0
投票

这就是您的配置-如果您有必填字段,则必须对查询进行分组

以下应完成的工作

您的控制器

public function product()
{
    $searchText="";
    if($this->input->post('searchText'))
    {
        $searchText = $this->input->post('searchText');
    }

    $arrFilter = [
        'like' =>   [
            'ProductName' => $searchText
        ],
        'orlike' => [
            'CategoryName' => $searchText,
            'SubCategoryName' => $searchText,
            'BrandName' => $searchText
        ],
    ];

    $jointables = [
        'categories'=>'CategorySlug=ProductCategory',
        'subcategories'=>'SubCategorySlug=ProductSubCategory',
        'brands'=>'BrandSlug=ProductBrand'
    ];


    $where = [
        'ProductIsActive'=>1,
        'ProductStatus'=>'Approved'
    ];

    $data['product']= $this->Custom_model-> getproduct('products',$jointables,$where,$arrFilter); 
}

您的模特

function getproduct($primarytable, $jointables, $where, $arrFilter = [])
{
    $this->db->select('*');
    $this->db->from($primarytable);
    $this->db->where($where);

    if (is_array($arrFilter) && count($arrFilter) > 0)
    {
        $this->db->group_start();

        if (isset($arrFilter['like']))
        {
            $this->db->like($arrFilter['like']));
        }

        if (isset($arrFilter['orlike']))
        {
            $this->db->group_start();
            foreach($arrFilter['orlike'] as $key=>$value)
            {
                if($value!="")
                {
                    $this->db->or_like($key,$value,'both');
                }           
            }
            $this->db->group_end();
        }

        $this->db->group_end();
    }

    foreach($jointables as $key=>$value)
    {
        $this->db->join($key,$value,'left');
    }       

    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $row = $query->result();
        return $row;
    }
    else
    {
        return FALSE;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.