Place part in parentheses

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();

What will result in:

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

But I need to return this with bedrooms >= ? OR suites >= ? in parentheses, how can I do this?

Ps: without using str_replace () .

    
asked by anonymous 07.05.2015 / 15:58

1 answer

4

Put the bedrooms condition in your array of OR :

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

Then adjust the part that mounts SQL like this:

if (!empty($what_or)) {
   $where .= ' AND (' . implode(' OR ', array_keys($what_or)) . ')';
   $conditions = array_merge($conditions, array_values($what_or));
}
    
07.05.2015 / 16:09