title: PHP Query based on 'select' values
body: <p>I have a form with multiple dropdowns consisting of multiple values. </p>
  
  <p>I'm trying to run a query based on what dropdown has been selected, so if only 1 has been filled out, run the first query, if 2 options have been filled out, run the second and so on and so forth. I'm sure there's a better way to do this only i'm not too proficient with HTML, any helps greatly appreciated...</p>
  
  <pre><code>&lt;!-- Search query --&gt;
  
      &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
          &lt;input type="hidden" name="search" value="complete"&gt;
          &lt;select name="OPT1"&gt;
              &lt;option value="#"&gt;Choose Your Fuel Type&lt;/option&gt;
              &lt;option value="petrol"&gt;Petrol&lt;/option&gt;
              &lt;option value="diesel"&gt;Diesel&lt;/option&gt;
              &lt;option value="lpg"&gt;LPG&lt;/option&gt;
          &lt;/select&gt;
  
          &lt;select name="OPT2"&gt;
              &lt;option value="#"&gt;Choose Your Fuel Type&lt;/option&gt;
              &lt;option value="petrol"&gt;Petrol&lt;/option&gt;
              &lt;option value="diesel"&gt;Diesel&lt;/option&gt;
              &lt;option value="lpg"&gt;LPG&lt;/option&gt;
          &lt;/select&gt;
  
          &lt;select name="OPT3"&gt;
              &lt;option value="#"&gt;Choose Your Fuel Type&lt;/option&gt;
              &lt;option value="petrol"&gt;Petrol&lt;/option&gt;
              &lt;option value="diesel"&gt;Diesel&lt;/option&gt;
              &lt;option value="lpg"&gt;LPG&lt;/option&gt;
          &lt;/select&gt;
          &lt;input type="submit" /&gt;
      &lt;/form&gt;
  
  
  
      &lt;?php 
          if ($_POST['OPT1'] != '') {     
  
          echo 'option 1';         
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } else if ($_POST['OPT2'] != '') {               
  
          echo 'option 2';
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } 
  
          else if ($_POST['OPT3'] != '') {                 
  
          echo 'option 3';
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' OR wpostmeta.meta_value = '".$_POST['OPT3']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } 
  
  
      ?&gt;
  </code></pre>
  
  <hr>
  
  <p>Ive got this working...</p>
  
  <pre><code>&lt;?php 
          if ($_POST['OPT2'] == '' &amp;&amp; $_POST['OPT3'] == '') {     
  
          echo 'option 1';         
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } else if ($_POST['OPT1'] != '' &amp;&amp; $_POST['OPT2'] != '' &amp;&amp; $_POST['OPT3'] == '') {               
  
          echo 'option 2';
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } 
  
          else if ($_POST['OPT1'] != '' &amp;&amp; $_POST['OPT2'] != '' &amp;&amp; $_POST['OPT3'] != '') {                 
  
          echo 'option 3';
          $pageposts = $wpdb-&gt;get_results("SELECT wposts.* FROM $wpdb-&gt;posts wposts, $wpdb-&gt;postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' OR wpostmeta.meta_value = '".$_POST['OPT3']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 
  
          } 
  
  
      ?&gt;
  </code></pre>
  
  <p>only when pulling my results it pulls really sporadically...</p>