多个复选框过滤器:MYSQL表(AJAX,PHP)

时间:2013-12-25 14:17:29

标签: php mysql ajax json checkbox

(抱歉我的英语不好)

我有一个包含不同类型手机的Mysql表,我想用几个类别的复选框过滤此列表。

我的代码工作正常,除了一件事......如果我选择了多个复选框......过滤器不起作用......例如:

如果我选择过滤......来自“三星”的所有手机......一切正常,相反,如果我选择过滤“三星”和“苹果”的所有手机...过滤器不再工作了。 ..什么也没有出现......只是一张白纸桌。

这是我的代码:

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);

这是脚本:

<div id="filter">
  <h2>Filter options</h2>

  <div><input type="checkbox" id="manufacturer" name="samsung"> <label for="manufacturer">samsung</label></div>
  <div><input type="checkbox" id="manufacturer" name="apple">   <label for="manufacturer">apple</label></div>
  <div><input type="checkbox" id="manufacturer" name="nokia">   <label for="manufacturer">nokia</label></div>
  <div><input type="checkbox" id="type" name="touchscreen"> <label for="type">touchscreen</label></div>
  <div><input type="checkbox" id="type" name="qwerty">      <label for="type">qwerty</label></div>
  <div><input type="checkbox" id="type" name="classic">     <label for="type">classic</label></div>
  <div><input type="checkbox" id="processor" name="single_core">        <label for="processor">single_core</label></div>
  <div><input type="checkbox" id="processor" name="dual_core">      <label for="processor">dual_core</label></div>
  <div><input type="checkbox" id="processor" name="quad_core">      <label for="processor">quad_core</label></div>

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script>
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k , v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";                 
 })

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();
</script> 

这里也是submit.php

<?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->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

0 个答案:

没有答案