I am working on a project in condeigniter(newbie). My project include listing of products using different filters like categories, subcategories, keywords, minimum-maximum prices etc. I have done this hundred of times in custom php, where i make variable `$where` for all conditions in `where` clause. For example

```
$where = '';
if($category != '')
{
  $where .= " category = '$category'";
}

```

Now in codeigniter I have multiple paramters,which I can get in my `controller` through `uri segment` and call `model` function, pass all parameters and `run mysql query` there. The problem is I am checking every parameter if it is empty or set and run multiple queries accordingly. For example.

```
if($category != '' && $subCategory != '')
{
  //whole query here
}
else  if($category == '' && $subCategory != '')
{
  //whole query here
}

//and so on

```

What I need is any optimize way of doing this, as I have joins (multiple paramters).

My sample conditions and queries

```
if($subCat == '' && $vendor == '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->where('categories.name',$cat);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else if($subCat == '' && $vendor != '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('vendors','vendors.id = listings.programme');
        $this->db->where('categories.name',$cat);
        $this->db->where('vendors.name',$vendor);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else if($subCat != '' && $vendor == '')
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('subcategories','subcategories.id = listings.subcategory');
        $this->db->where('categories.name',$cat);
        $this->db->where('subcategories.name',$subCat);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();
        return $query->result();
    }
    else
    {
        $cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
        $subCat = str_replace("-"," ",str_replace("-and-"," & ",$subCat));
        $vendor = str_replace("-"," ",str_replace("-and-"," & ",$vendor));

        $this->db->select('listings.* ');
        $this->db->from('listings');
        $this->db->join('categories','categories.id = listings.category');
        $this->db->join('subcategories','subcategories.id = listings.subcategory');
        $this->db->join('vendors','vendors.id = listings.programme');
        $this->db->where('categories.name',$cat);
        $this->db->where('subcategories.name',$subCat);
        $this->db->where('vendors.name',$vendor);
        $this->db->limit($perpage,$page);
        $query = $this->db->get();  
        return $query->result();
    }

```