title: php mysql adding values to array based on IF statements
body: <p>Using Joomla, having issues trying to build a mySQL query based on URL arguments.  The code I have looks like this:</p>
  
  <pre><code>    $db =&amp; JFactory::getDBO();
      $hpprice = JRequest::getVar('hprice');
      $lprice = JRequest::getVar('lprice');
      $city = JRequest::getVar('city');
      $zip = JRequest::getVar('zip');
      $bdrms = JRequest::getVar('bdrms');
      $bths = JRequest::getVar('bths');
  
      $query = "SELECT * FROM " . $db-&gt;nameQuote('#__mls') . " WHERE 1=1";
      $clauses = array();
      if ($zip != null) {
          $clauses[] = $db-&gt;nameQuote('MSTZIP') . " = " . $db-&gt;quote($zip);
      }
      if ($city != null) {
          $clauses[] = $db-&gt;nameQuote('MSTCITY') . " = '" . $db-&gt;quote($city) . "'";
      }
      if ($bdrms != null){
          $clauses[] = $db-&gt;nameQuote('MSTBDRMS')." &gt;= ".$db-&gt;quote($bdrms);
      }
      if ($bths != null){
          $clauses[] = $db-&gt;nameQuote('MSTBATHS') . " &gt;= " . $db-&gt;quote($bths);
      }
      if ($lprice != null){
          $clauses[] = $db-&gt;nameQuote('MSTLISTPRC') . " BETWEEN " . $db-&gt;quote($lprice) . " AND " . $db-&gt;quote($hprice);
      } 
  
      $query .= implode(" AND ", $clauses);
  
      $db-&gt;setQuery($query);
      $table = $db-&gt;loadRowList();
      return $table;
  </code></pre>
  
  <p>So, as you can see it, adds arguments to the mySQL query based on whether or not arugments exist in the URL.  What I can't wrap my head around is building the array and imploding it.  </p>
  
  <p>Whenever I put an argument in the URL, all the table items populate.  When I try to pass an argument, it comes up null.  You can see this in action <a href="http://mgoode.com/index.php?option=com_mls&amp;view=list" rel="nofollow">here</a>.  If you add another argument like <code>zip</code> to the URL, everything comes up <code>NULL</code>.</p>