title: CodeIgniter conditional statement inside query
body: <p>This is a simplified version of the code I have:</p>
  
  <pre><code>    $this-&gt;db-&gt;select('column1, column2, status, etc');
  
      if ($search['option_1']){
          $this-&gt;db-&gt;or_where('status', 15);
          $this-&gt;db-&gt;or_where('status', 20);
      }
  
      if ($search['option_2']){
          $this-&gt;db-&gt;or_where('status', 30);
          $this-&gt;db-&gt;or_where('status', 40);
          $this-&gt;db-&gt;or_where('status', 50);
      }
  
      if ($search['option_3']){
          $this-&gt;db-&gt;or_where('status', 99);
      }
  
      $this-&gt;db-&gt;where('column', $search['value']);
      etc..
  </code></pre>
  
  <p>On the webpage (a search form) I have, among other fields and select boxes, three checkboxes: option_1 to option_3. All checkboxes are optional. Each checkbox correspond to a status.  </p>
  
  <p>The checkboxes can be all checked, some checked or no checkbox checked.</p>
  
  <p>I think the problem with this code is that brackets are missing around the OR statements and therefore "breaks" the query. Do I have to make:</p>
  
  <pre><code>    if ($search['option_1']){
              $this-&gt;db-&gt;where('(status = 15 OR status = 20)');
      }
      if ($search['option_1'] &amp;&amp; $search['option_2']){
              $this-&gt;db-&gt;where('(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50)');
      }
      etc...
  </code></pre>
  
  <p>or is there a smarter way?</p>
  
  <p>[UPDATE]<br>
  I'm using this until someone comes up with a better way:</p>
  
  <pre><code>   if($search['option_1'] || $search['option_2'] || $search['option_3']) {
          if ($search['option_1']){
              $status = '(status = 15 OR status = 20)';
          }
          else if ($search['option_2']){
              $status = '(status = 30 OR status = 40 OR status = 50)';
          }
          else if($search['option_3']) {
              $status = 'hr_olas.k_VALU_Status = 99';
          }
           else if($search['option_1'] &amp;&amp; $search['option_2']) {
              $status = '(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50)';
          }
           else if($search['option_1'] &amp;&amp; $search['option_3']) {
              $status = '(status = 15 OR status = 20 OR status = 99)';
          }
           else if($search['option_2'] &amp;&amp; $search['option_3']) {
              $status = '(status = 30 OR status = 40 OR status = 50 OR status = 99)';
          }
           else if($search['option_1'] &amp;&amp; $search['option_2'] &amp;&amp; $search['option_3']) {
              $status = '(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50 OR status = 99)';
          }
  
          $this-&gt;db-&gt;where($status);
      }
  </code></pre>