title: Filtering PHP with checkboxes
body: <p>I am working on this script that will filter sql results based on the which checkboxes are checked.  My table is set up like so..</p>
  
  <pre><code>id   venue    imageurl    showingads    2kandunder  2kto4k   4kandup
  1    venue1   myurl.com    yes           yes
  2    venue2   myurl.com    yes                       yes
  3    venue3   myurl.com    no                                  yes
  4    venue4   myurl.com    yes           yes
  </code></pre>
  
  <p>All code is on the same page.  Here is the html..</p>
  
  <pre><code>&lt;form id="form" method="post" action=""&gt;
  &lt;input type="checkbox" name="2kandunder" class="checkbox" &lt;?=(isset($_POST['2kandunder'])?' checked':'')?&gt;/&gt; 2kandunder&lt;br&gt;
  &lt;input type="checkbox" name="2kto4k" class="checkbox" &lt;?=(isset($_POST['2kto4k'])?' checked':'')?&gt;/&gt; 2k to 4k&lt;br&gt;
  &lt;input type="checkbox" name="4kandup" class="checkbox" &lt;?=(isset($_POST['4kandup'])?' checked':'')?&gt;/&gt; 4k and up&lt;br&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript code...</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;  
  $(function(){
   $('.checkbox').on('change',function(){
      $('#form').submit();
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>and php...</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";
  $dbname = "mydb";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
   if ($conn-&gt;connect_error) {
   die("Connection failed: " . $conn-&gt;connect_error);
   }
  
  if (isset($_POST["2kandunder"])) {
  $arguments[] = "`2kandunder` = 'yes'";
  }
  if (isset($_POST["2kto4k"])) {
  $arguments[] = "`2kto4k` LIKE 'yes'";
  }
  if (isset($_POST["4kandup"])) {
  $arguments[] = "4kandup LIKE '%yes%'";
  }
  if(!empty($arguments)) {
  $str = implode(' or ',$arguments);
  
  $qry = "SELECT id, venue, imageurl FROM ads where " . $str . "";
  
  $paginate = new pagination($page, $qry, $options);
  } else {
  //Whatever happens when there's none checked.
  $sql = "SELECT id, venue, imageurl FROM ads WHERE `showingads` = 'yes'";
  }
  
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
  // output data of each row
  while($row = $result-&gt;fetch_assoc()) {
      echo "&lt;table class='tablebox' width='940' border='1' cellspacing='5'&gt;
      &lt;tr&gt;&lt;td width='75'&gt;".$row["venue"]."&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;
      &lt;td width='10'&gt;&lt;a href='post_click.php?id=".$row["id"]."'&gt; &lt;img src='".$row["imageurl"]."'&gt;&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;"; 
  
  
    }
  echo "&lt;/table&gt;";
  } else {
  echo "0 results";
  }
  $conn-&gt;close();
  
  ?&gt;
  </code></pre>
  
  <p>When I run this page else query displays which is what I want to happen when page first loads.  But when I check any of the checkboxes nothing happens. Here is entire code as it is on page. </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;form id="form" method="post" action=""&gt;
  &lt;input type="checkbox" name="2kandunder" class="checkbox" &lt;?=(isset($_POST['2kandunder'])?' checked':'')?&gt;/&gt; 2k and under&lt;br&gt;
  &lt;input type="checkbox" name="2kto4k" class="checkbox" &lt;?=(isset($_POST['2kto4k'])?' checked':'')?&gt;/&gt; 2k to 4k&lt;br&gt;
  &lt;input type="checkbox" name="4kandup" class="checkbox" &lt;?=(isset($_POST['4kandup'])?' checked':'')?&gt;/&gt; 4k and up&lt;br&gt;
   &lt;/form&gt;
  &lt;script type="text/javascript"&gt;  
  $(function(){
   $('.checkbox').on('change',function(){
      $('#form').submit();
      });
  });
  &lt;/script&gt;
  
  &lt;?php
  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";
  $dbname = "mydb";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
   if ($conn-&gt;connect_error) {
   die("Connection failed: " . $conn-&gt;connect_error);
  }
  
  if (isset($_POST["2kandunder"])) {
   $arguments[] = "`2kandunder` = 'yes'";
  }
  if (isset($_POST["2kto4k"])) {
  $arguments[] = "`2kto4k` LIKE 'yes'";
  }
  if (isset($_POST["4kandup"])) {
  $arguments[] = "4kandup LIKE '%yes%'";
  }
  if(!empty($arguments)) {
   $str = implode(' or ',$arguments);
  
   $qry = "SELECT id, venue, imageurl FROM ads where " . $str . "";
  
   $paginate = new pagination($page, $qry, $options);
  } else {
  //Whatever happens when there's none checked.
  $sql = "SELECT id, venue, imageurl FROM ads WHERE `showingads` = 'yes'";
  }
  
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
  // output data of each row
  while($row = $result-&gt;fetch_assoc()) {
      echo "&lt;table class='tablebox' width='940' border='1' cellspacing='5'&gt;
      &lt;tr&gt;&lt;td width='75'&gt;".$row["venue"]."&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;
      &lt;td width='10'&gt;&lt;a href='post_click.php?id=".$row["id"]."'&gt; &lt;img src='".$row["imageurl"]."'&gt;&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;"; 
  
  
   }
  echo "&lt;/table&gt;";
  } else {
  echo "0 results";
  }
  $conn-&gt;close();
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My first thoughts is 1.  The javascript isn't executing at all?  2. My $arguments is formatted wrong for how I have the database set up? (I have 3 different variations for the arguments to try and test out different formats to see if I could get one to work)</p>
  
  <p>Note:  The php portion from tutorial I learned from had the argument like if (isset($_POST["2kandunder"]).  I have it like this: if (isset($_POST["2kandunder"])) with the extra parenthesis at the end because I was getting a syntax error for each if statement. If that could be the problem why was I getting syntax errors for each if statement line?</p>
  
  <p>All this code and blabbering but I feel like I am close.  Can someone PLEASE help me figure out what the problem is?  </p>
  
  <p><strong>EDIT</strong> 
  columns are set up like this..</p>
  
  <pre><code>Field    Type          Null    Key    Default     Extra  
  
  id       int(5)         NO     PRI     NULL     auto_increment 
  
  venue   varchar(100)    NO             NULL 
  
  imageurl varchar(150)   NO             NULL 
  
  showingads varchar(5)   NO             NULL 
  
  2kandunder varchar(5)   NO             NULL 
  
  2kto4k  varchar(5)      NO             NULL 
  
  4kandup varchar(5)      NO             NULL 
  </code></pre>
  
  <p><strong>UPDATE</strong>
  So I have the checkboxes working now but now when I load the page and click the top checkbox (2kandunder) it returns 0 results. The else sql still works for when no checkboxes are checked.  I changed the first if statement to </p>
  
  <pre><code>if (isset($_POST["2kandunder"])) {
  $arguments[] = "`showingads` = 'yes'";
  </code></pre>
  
  <p>which should make it the same query as the else sql query when the first (2kandunder) box is checked.  Still 0 results shows.  So I echoed the $qry to display what query was being run when the first checkbox is checked and it is the exact same as the else $sql query but still returns 0 results?  How is this possible if that exact same query works for the else query?</p>
  
  <p><strong>Last Update</strong>
  The problem was the if $results it was only showing results for the sql query thanks for anyone that helped!</p>