I am using codeigniter for my database application. I am not sure if it is possible to use get_where for custom query in codeigniter database class.
This is my query
$this->db->query("select model, varient, (select color from mtbl_colors where mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles");
Now if i want to filter the above using where clause, I am using the query as
function getVehicles($model='',$varient='')
{
if($model!='')
{
$q=$this->db->query("select model,varient,(select color from mtbl_colors where
mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles
where model='$model'")->result();
return $q;
}
elseif($varient!='')
{
$q=$this->db->query("select model,varient,(select color from mtbl_colors where
mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles
where varient='$varient'")->result();
return $q;
}
}
This is only an example, where i have to write all conditions for each where conditions. So, I may be missing some thing in codeigniter which can accomplish such multiple where conditions more easier than i am using right now.
EDIT::::
after suggestion from @Yan I tried as follows
function getVehicles($where_clause='',$where_value='') {
$sql = "select model,varient,(select color from mtbl_colors where
mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles";
$where_clause=array("varient", "model", "colorid");
$where_value=array("MAGNA 1.4", "SANTRO", "3")
if (!empty($where_clause) && !empty($where_value)) {
$sql .= " where $where_clause = ?";
return $this->db->query($sql, $where_value)->result();
}
return false;
}
I received a database error saying "invalid column value Aarray"
as my intention was to implement multiple condition to generate results as per my filter options.