title: How do I make filters work together using AJAX, PHP and MySQL
body: <p>I have this table that shows a list of users and I'm trying to create some filters with selectbox to filter with some parameters. I'm building this table with a PHP script that I call with AJAX. This AJAX is activated when the user click on one of this selectbox.</p>
  
  <p>This are the filters:</p>
  
  <pre><code>&lt;div class="col-lg-6"&gt;                                
     &lt;select class="form-control filter" name="category" id="category"&gt;
         &lt;option&gt;All&lt;/option&gt;
         &lt;option&gt;Abraham&lt;/option&gt;
         &lt;option&gt;Kevin&lt;/option&gt;
         &lt;option&gt;Eric&lt;/option&gt;
     &lt;/select&gt;                                
  &lt;/div&gt;   
  &lt;div class="col-lg-6"&gt;                                
     &lt;select class="form-control filter" name="datePeriod" id="datePeriod"&gt;
         &lt;option&gt;Today&lt;/option&gt;
         &lt;option&gt;Yesterday&lt;/option&gt;
         &lt;option&gt;Last 7 days&lt;/option&gt;
         &lt;option&gt;Last month&lt;/option&gt;
         &lt;option&gt;Last year&lt;/option&gt;
     &lt;/select&gt;                                
  &lt;/div&gt;
  &lt;div class="col"&gt;
     &lt;div id="output"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and this is the AJAX:</p>
  
  <pre><code>$(document).ready(function(){
      $('.filter').each(function () {
          $(this).change(function(){ 
              var filters = $(this).val(); 
              var dataString = "filters="+filters;
  
              $.ajax({
                  type: "GET", 
                  url: "processes/filters.php", 
                  data: dataString, 
                  success: function(result){ 
                      $("#output").html(result);
                  }
              });
          });            
      });
      $("#datePeriod").trigger("change");
  });
  </code></pre>
  
  <p>This is my PHP code:</p>
  
  <pre><code>&lt;?php
  
      //Getting $datePeriod value for query
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Today" &amp;&amp; $_GET['filters'] == "All"){
          $datePeriod = 'AND DATE(VISIT_DATE) = CURDATE()';
          $category = "";
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Yesterday"){
          $datePeriod = 'AND VISIT_DATE &gt;= DATE(NOW()) - INTERVAL 1 DAY';     
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Last 7 days"){
          $datePeriod = 'AND VISIT_DATE &gt;= DATE(NOW()) - INTERVAL 7 DAY';
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Last month"){
          $datePeriod = 'AND VISIT_DATE &gt;= DATE(NOW()) - INTERVAL 1 MONTH';
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Last year"){
          $datePeriod = 'AND VISIT_DATE &gt;= DATE(NOW()) - INTERVAL 1 YEAR';
      }
  
      //Getting $category value for query
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "All"){
          $category = "";
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Abraham"){
          $category = "AND VISIT_REASON='Abraham'";
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Kevin"){
          $category = "AND VISIT_REASON='Kevin'";
      }
      if(isset($_GET['filters']) &amp;&amp; $_GET['filters'] == "Eric"){
          $category = "AND VISIT_REASON='Eric'";
      }
  
      include('conn.php');
  
      mysqli_set_charset($conn, 'utf8');
  
      $orderBy = 'ORDER BY VISIT_DATE DESC';
  
      $sql = "SELECT * FROM queue WHERE ATTENDED='1' ".$category." ".$datePeriod." ".$orderBy." ";
  
      $result = mysqli_query($conn, $sql);
  
      //$ID = 'ID';
      $GUEST_NAME = 'GUEST_NAME';
      $GUEST_PHONE = 'GUEST_PHONE';
      $VISIT_REASON = 'VISIT_REASON';
      $VISIT_DATE = 'VISIT_DATE';
  
      echo '&lt;table class="table table-striped"&gt;';
      echo "&lt;tr&gt;";
      //echo '&lt;th scope="col"&gt;ID&lt;/th&gt;';
      echo '&lt;th scope="col"&gt;Name&lt;/th&gt;';
      echo '&lt;th scope="col"&gt;Phone&lt;/th&gt;';
      echo '&lt;th scope="col"&gt;Attended by&lt;/th&gt;';
      echo '&lt;th scope="col"&gt;Visit Date&lt;/th&gt;';
      echo "&lt;/tr&gt;";
  
      if (mysqli_num_rows($result) &gt; 0) {
  
          while($row = mysqli_fetch_assoc($result)) {             
  
              echo "&lt;tr&gt;";
              //echo "&lt;td&gt;$row[$ID]&lt;/td&gt;";
              echo "&lt;td&gt;$row[$GUEST_NAME]&lt;/td&gt;";
              echo "&lt;td&gt;$row[$GUEST_PHONE]&lt;/td&gt;";
              echo "&lt;td&gt;$row[$VISIT_REASON]&lt;/td&gt;";
              echo "&lt;td style='text-transform:uppercase;'&gt;".date('M-d-Y g:i a', strtotime(str_replace('-','/', $row['VISIT_DATE'])))."&lt;/td&gt;";
              echo "&lt;/tr&gt;";
  
          }
  
      } else {
          echo "&lt;td style='text-transform: none;'&gt;No records to show.&lt;/td&gt;";
      }
  
      echo '&lt;/table&gt;';
  
      mysqli_close($conn);
  
  ?&gt;
  </code></pre>
  
  <p>My problem is that I can't figure out how to build the correct conditions so the two filters can work together. I need that when the user select one filter the query build it self considering the value of the other one.</p>
  
  <p>When I select the category filter I get "Undefined variable: datePeriod".
  When I select the datePeriod filter I get "Undefined variable: category".</p>
  
  <p>I do not know if I'm doing it the wrong way. Maybe I'm not building the query in the correct way. I will appreciate any help or guidance.</p>