0

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>';
}
3
  • 1
    i would stick to the original syntax, why change it? Commented Feb 10, 2016 at 21:23
  • in your case may be array_filter can helps.. something like $clauses = array_filter($clauses, function($v){return !is_null($v);}); after all ternary operators. Commented Feb 10, 2016 at 21:31
  • ugly style code: $clauses = array_merge( $clauses, $catId ? array('`category`='.$catId) : array() ) Commented Feb 10, 2016 at 21:47

3 Answers 3

4

Yes. If you really want to use a ternary, it is possible like this:

$catId ? $clauses[] = "`category`=$catId" : null;

Here the assignment happens in the true portion of the ternary instead of assigning the result of the ternary.

Using a ternary is kind of pointless, though, because you don't want to do anything if the variable is empty, so the false portion of the ternary is irrelevent. A shorter version that should have basically the same effect is:

$catId && $clauses[] = "`category`=$catId";

Personally, I think the way you already have it is more readable and friendlier to anyone who has to work on this later by being less weird and hacky, but it is possible.

Sign up to request clarification or add additional context in comments.

Comments

1

Of course, you can do this with ternary operator as @Dontpanic suggested. But you should be more aware of SQL injection so I would recommend using prepared statement instead of substitute values by yourself as following:

$where = null;

/// Generic query
$sql = "SELECT * FROM `products` WHERE 1 = 1"

$params = array();

if($manId){
    $sql .= " AND `man`= ?";
    $params[] = $manId;
}

if($catId){
    $sql .= " AND `category` = ?"
    $params[] = $catId;
}

/// Adding ORDER BY clause
$sql .=  " ORDER BY `isfeatured`,`sortvalue`";

/// Prepare query
$stmt = mysqli_prepare ($con, $sql);

foreach($params as $param) {
    /// Binding integer parameters
    $stmt->bind_param("i", $param);
}

/// Execute the statement
$stmt->execute();

/// Get results
$result = $stmt->get_result();

/// Process the results
while($row = $result->fetch_assoc()){
    print $row['name'].'<br>';
}

1 Comment

This is very nice, ultimately where I was headed (using prepared statements). However, I was liking the use of implode as opposed to 1==1, even though there is not much wrong with the 1==1 method, just like not having to use that meaningless statement.
0

This looks fine:

$clauses[] = ($catId)?'`category` = '.$catId:null;

If $catId is not false (read about casting types in PHP) new elem in array will be a string with value equal 'category = '.$catId, otherwise null, but always you added new elemnt to array in this place. So your rest of code will be not working. Especially this part:

if(count($clauses)){
$where = implode (' && ',$clauses);
$where = 'WHERE '.$where;
}

Because you will get for example something like this: WHERE category = 1 AND ORDER BY

2 Comments

There is will be no NULL in the string... it will be just where `category`=1 and order by ... .. it's syntax error :)
Of course, I wrote it quickly and made a obious error, but conclusion is the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.