title: Grouping WHERE clauses in Codeigniter
body: <p>I want to produce the following SQL code using Active Records in Codeigniter:</p>
  
  <pre><code>WHERE name != 'Joe' AND (age &lt; 69 OR id &gt; 50)
  </code></pre>
  
  <p>Doing the following seems to be as far as I can get, I cant figure out how to group them</p>
  
  <pre><code>$this-&gt;db-&gt;select()-&gt;from('users')-&gt;where('name !=', 'Joe')-&gt;where('age &lt;', 69)-&gt;or_where('id &lt;', $id); 
  </code></pre>
  
  <p>Any ideas? My SQL query is too complex so I dont wish to rewrite everything in traditional SQL.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My SQL code is dynamically generated depending on the values of certain parameters passed into the model method. The problem with not being able to use parenthesis causes a problem because the operator precedence is such that <code>AND</code> is evaluated first before <code>OR</code>. </p>
  
  <p>*Here is a chunk of my active records code, where there are some other code before and after it:</p>
  
  <pre><code>            ... some $this-&gt;db-&gt;where() ...
              ... some $this-&gt;db-&gt;where() ...
  
      if($price_range) {
          $price_array = explode('.', $price_range);
          for($i = 0; $i &lt; count($price_array); $i++) {
              if($i == 0) {
                  $this-&gt;db-&gt;where('places.price_range', $price_array[$i]);
              } else {
                  $this-&gt;db-&gt;or_where('places.price_range', $price_array[$i]);
              }
          }
  
      }
  
              ... some $this-&gt;db-&gt;where() ...
              ... some $this-&gt;db-&gt;where() ...
  </code></pre>
  
  <p>The problem comes because I am using <code>$this-&gt;db-&gt;or_where()</code> which introduces a <code>OR</code> clause that throws the operator precedence into disarray without being able to use <code>( )</code> to change the order.</p>
  
  <p>** Is there any way to solve this? **</p>