Is it possible to write this as a ternary operator?
if($catId){
$clauses[] ='`category` = '.$catId;
}
When I try the following I still get a value added to the array
$clauses[] = ($catId)?'`category` = '.$catId:null;
For reference, Im using this in building a sql query with a where clause
$where = null;
$clauses = array();
if($manId){
$clauses[] ='`man` = '.$manId;
}
if($catId){
$clauses[] ='`category` = '.$catId;
}
if(count($clauses)){
$where = implode (' && ',$clauses);
$where = 'WHERE '.$where;
}
$sql = "SELECT * FROM `products` $where ORDER BY `isfeatured`,`sortvalue`";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
print $row['name'].'<br>';
}
array_filtercan helps.. something like$clauses = array_filter($clauses, function($v){return !is_null($v);});after all ternary operators.$clauses = array_merge( $clauses, $catId ? array('`category`='.$catId) : array() )