Put stretch between parentheses

Asked

Viewed 132 times

2

I have this code:

if(isset($params['bedrooms']) && is_numeric($params['bedrooms']) && $params['bedrooms'] > 0){
    $what['bedrooms >= ?']  = $params['bedrooms'];
    $what_or['suites >= ?'] = $params['bedrooms'];
 }

 $where = implode(' AND ',array_keys($what));
 $conditions = array_values($what);
 if (!empty($what_or)) {
   $where .= ' OR ' . implode(' OR ', array_keys($what_or));
   $conditions = array_merge($conditions, array_values($what_or));
 }
 pr($where);
 die();

That will result me in:

modality_id = ? AND property_type_id = ? AND properties.city_id = ? AND status = ? AND bedrooms >= ? OR suites >= ?

But I must return this with bedrooms >= ? OR suites >= ? between parentheses, how can I do this?

Ps: not to be used str_replace().

1 answer

4


Put the condition of bedrooms in your array of OR:

$what_or['bedrooms >= ?']  = $params['bedrooms'];
$what_or['suites >= ?'] = $params['bedrooms'];

Then adjust the part that mounts the SQL like this:

if (!empty($what_or)) {
   $where .= ' AND (' . implode(' OR ', array_keys($what_or)) . ')';
   $conditions = array_merge($conditions, array_values($what_or));
}
  • Thank you for the reply! So I got this as a result : modality_id = ? AND property_type_id = ? AND properties.city_id = ? AND status = ? AND garage >= ? AND bedrooms >= ? OR (suites >= ?) when I need it to work like this modality_id = ? AND property_type_id = ? AND properties.city_id = ? AND status = ? AND garage >= ? AND (bedrooms >= ? OR suites >= ?)


  • I updated my reply @GWER, see if you solved it.

  • It worked! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.