title: Altering mysql query based on variable
body: <p>So I am making a click based search tool. Nothing fancy and it works great as long as you pick at least one thing from all the available categories. I want to change it so that there would be no mandatory categories to pick from, so if I only want to pick from category <code>cpu</code>, then that would give results aswell.</p>
  
  <p>This is what i have as input:</p>
  
  <pre><code>&lt;form action="search.php" method="GET"&gt; 
  &lt;input value="i3"  name="cpu[]" type="checkbox"&gt;i3 
  &lt;input value="i5"  name="cpu[]" type="checkbox"&gt;i5 &lt;br&gt;
  &lt;input value="4gb"  name="ram[]" type="checkbox"&gt;4gb  
  &lt;input value="8gb"  name="ram[]" type="checkbox"&gt;8gb&lt;br&gt;
  &lt;input value="1tb"  name="storage[]" type="checkbox"&gt;1TB 
  &lt;input value="500gb"  name="storage[]" type="checkbox"&gt;500GB&lt;br&gt;
  &lt;input type="submit" value="search" class="submit" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is the part that gathers the clicked checkboxes (I left out the cpu and ram parts since its the same):</p>
  
  <pre><code>if(isset($_GET['storage'])){
    if (is_array($_GET['storage'])) {
      foreach($_GET['storage'] as $storage_value){
        $storage_term = trim($storage_value);
        $storage_term_array[] = $storage_term;
      if (!empty($storage_term)) {
          $storage_bits[] = "`storage` LIKE '%$storage_term%'";
      }
  } } }
  else $storage_bits="0";
  </code></pre>
  
  <p>And the <code>mysql_query</code> goes like this:</p>
  
  <pre><code>$res=mysql_query("SELECT * FROM laptop WHERE 
  (".implode(' OR ', $cpu_bits).")  
  AND (".implode(' OR ', $ram_bits).") 
  AND (".implode(' OR ', $storage_bits).")
  AND visibility=1");
  </code></pre>
  
  <p>This is what it returns:</p>
  
  <pre><code>(`cpu` LIKE '%i5%') AND (`ram` LIKE '%8gb%') AND (`storage` LIKE '%1tb%' OR `storage` LIKE '%500gb%')
  </code></pre>
  
  <p>This is the code that is working, but as I said before you need to pick at least 1 checkbox per category. How do I completely exclude lets say the <code>AND (".implode(' OR ', $storage_bits)."</code> part <code>if $storage_bits="0"</code>?</p>
  
  <p>So far I have had no luck with <code>case</code></p>
  
  <pre><code>$res=mysql_query("SELECT * FROM laptop WHERE 
  (".implode(' OR ', $cpu_bits).")  
  AND (".implode(' OR ', $ram_bits).") 
  (case when ($storage_bits=0) THEN 'AND 1=1' ELSE 'AND (".implode(' OR ', $storage_bits)."' END )
  AND visibility=1");
  </code></pre>
  
  <p>And not even the <code>IF</code> is working for me.</p>
  
  <pre><code>$res=mysql_query("SELECT * FROM laptop WHERE 
  (".implode(' OR ', $cpu_bits).")  
  AND (".implode(' OR ', $ram_bits).") 
  IF($storage_bits=0,'1=1','AND (".implode(' OR ', $storage_bits).")
  AND visibility=1");
  </code></pre>
  
  <p>It gives me no syntax errors whatsoever, it just doesn't give results. What gives? I feel like I am missing some quotation marks or something similarly pitiful but I just cant seem to wrap my head around it. Thanks!</p>