title: Multiple checkbox filter: MYSQL table (AJAX, PHP)
body: <p>(and sorry for my bad english)</p>
  
  <p>I have a Mysql table with different kinds of mobile phones and i want to filter this list with checkboxes for several categories. </p>
  
  <p>My code is working fine, except one thing... if i select more then one checkboxes ... the filtre is not working...for example:</p>
  
  <p>If i select to filter ... all phones from "Samsung" ... everything is ok, instead if i select to filter all phones from "Samsung" and "Apple" ... the filter is not working anymore...and nothing show up...just a blanc table.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `phone_name` varchar(255) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `samsung` tinyint(1) DEFAULT NULL,
  `apple` tinyint(1) DEFAULT NULL,
  `nokia` tinyint(1) DEFAULT NULL,
  `touchscreen` tinyint(1) DEFAULT NULL,
  `qwerty` tinyint(1) DEFAULT NULL,
  `classic` tinyint(1) DEFAULT NULL,
  `single_core` tinyint(1) DEFAULT NULL,
  `dual_core` tinyint(1) DEFAULT NULL,
  `quad_core` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  INSERT INTO `test_table` (`id`, `phone_name`, `price`, `samsung`, `apple`, 
  `nokia`, `touchscreen`, `qwerty`, `classic`, `single_core`, `dual_core`, `quad_core`)     
  VALUES
  (1, 'Samsung Galaxy S4', 470, 1, 0, 0, 1, 0, 0, 0, 0, 1),
  (2, 'Samsung Chat', 220, 1, 0, 0, 1, 1, 0, 1, 0, 0),
  (3, 'Iphone 4', 380, 0, 1, 0, 1, 0, 0, 0, 1, 0),
  (4, 'Iphone 5', 550, 0, 1, 0, 1, 0, 0, 0, 0, 1),
  (5, 'Nokia Lumia 520', 150, 0, 0, 1, 1, 0, 0, 0, 1, 0),
  (6, 'Nokia E72', 250, 0, 0, 1, 0, 1, 0, 1, 0, 0);
  </code></pre>
  
  <p>Here is the script:</p>
  
  <pre><code>&lt;div id="filter"&gt;
    &lt;h2&gt;Filter options&lt;/h2&gt;
  
    &lt;div&gt;&lt;input type="checkbox" id="manufacturer" name="samsung"&gt; &lt;label for="manufacturer"&gt;samsung&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="manufacturer" name="apple"&gt;   &lt;label for="manufacturer"&gt;apple&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="manufacturer" name="nokia"&gt;   &lt;label for="manufacturer"&gt;nokia&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="type" name="touchscreen"&gt; &lt;label for="type"&gt;touchscreen&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="type" name="qwerty"&gt;      &lt;label for="type"&gt;qwerty&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="type" name="classic"&gt;     &lt;label for="type"&gt;classic&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="processor" name="single_core"&gt;        &lt;label for="processor"&gt;single_core&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="processor" name="dual_core"&gt;      &lt;label for="processor"&gt;dual_core&lt;/label&gt;&lt;/div&gt;
    &lt;div&gt;&lt;input type="checkbox" id="processor" name="quad_core"&gt;      &lt;label for="processor"&gt;quad_core&lt;/label&gt;&lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; 
  &lt;script&gt;
  function makeTable(data){
  var tbl_body = "";
  $.each(data, function() {
  var tbl_row = "";
  $.each(this, function(k , v) {
  tbl_row += "&lt;td&gt;"+v+"&lt;/td&gt;";
  })
  tbl_body += "&lt;tr&gt;"+tbl_row+"&lt;/tr&gt;";                 
   })
  
  return tbl_body;
  }
  function getEmployeeFilterOptions(){
  var opts = [];
  $checkboxes.each(function(){
  if(this.checked){
  opts.push(this.name);
  }
  });
  
  return opts;
  }
  
  function updateEmployees(opts){
  $.ajax({
  type: "POST",
  url: "submit.php",
  dataType : 'json',
  cache: false,
  data: {filterOpts: opts},
  success: function(records){
  $('#mobile_phones tbody').html(makeTable(records));
  }
  });
  }
  
  var $checkboxes = $("input:checkbox");
  $checkboxes.on("change", function(){
  var opts = getEmployeeFilterOptions();
  updateEmployees(opts);
  });
  
  updateEmployees();
  &lt;/script&gt; 
  </code></pre>
  
  <p>And also here is the submit.php</p>
  
  <pre><code>&lt;?php 
  $pdo = new PDO('mysql:host=localhost;dbname=test_database', 'root', '');
  $select = 'SELECT id,phone_names,price';
  $from = ' FROM test_table';
  $where = ' WHERE TRUE';
  $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
  
  
  if (in_array("samsung", $opts)){
  $where .= " AND samsung = 1";
  } 
  if (in_array("apple", $opts)){
  $where .= " AND apple = 1";
  } 
  if (in_array("nokia", $opts)){
  $where .= " AND nokia = 1";
  } 
  if (in_array("touchscreen", $opts)){
  $where .= " AND touchscreen = 1";
  } 
  if (in_array("qwerty", $opts)){
  $where .= " AND qwerty = 1";
  } 
  if (in_array("classic", $opts)){
  $where .= " AND classic = 1";
  } 
  if (in_array("single_core", $opts)){
  $where .= " AND single_core = 1";
  }   
  if (in_array("dual_core", $opts)){
  $where .= " AND dual_core = 1";
  }   
  if (in_array("quad_core", $opts)){
  $where .= " AND quad_core = 1";
  }   
  
  $sql = $select . $from . $where;
  $statement = $pdo-&gt;prepare($sql);
  $statement-&gt;execute();
  $results = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
  $json = json_encode($results);
  echo($json);
  ?&gt;
  </code></pre>