Javascript 在PHP中使用多个复选框进行过滤
我想用PHP做过滤。我已经这样做了,但在性别类别,它是像单选按钮的工作。当我同时点击男性和女性时,将找不到任何记录。所以请任何人都能解决我的问题 我的代码在这里: index.phpJavascript 在PHP中使用多个复选框进行过滤,javascript,php,html,Javascript,Php,Html,我想用PHP做过滤。我已经这样做了,但在性别类别,它是像单选按钮的工作。当我同时点击男性和女性时,将找不到任何记录。所以请任何人都能解决我的问题 我的代码在这里: index.php <h1>Snowboards</h1> <table id="snowboards"> <thead> <tr> <th>ID</th> <th>Gender</th>
<h1>Snowboards</h1>
<table id="snowboards">
<thead>
<tr>
<th>ID</th>
<th>Gender</th>
<th>Price Range</th>
<th>Brand</th>
<th>Model</th>
<th>Rocker Type</th>
<th>Flex</th>
<th>Size Range</th>
<th>Image Path</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id="filter">
<h2>Filter options</h2>
<h3>Gender</h3>
<div>
<input type="checkbox" id="male" name="male">
<label for="male">Male</label>
</div>
<div>
<input type="checkbox" id="female" name="female">
<label for="female">Female</label>
</div>
<h3>Brand</h3>
<div>
<input type="checkbox" id="xyz" name="xyz">
<label for="xyz">xyz</label>
</div>
<div>
<input type="checkbox" id="burton" name="burton">
<label for="burton">burton</label>
</div>
<div>
<input type="checkbox" id="abc" name="abc">
<label for="abc">abc</label>
</div>
<div>
<input type="checkbox" id="www" name="www">
<label for="www">www</label>
</div>
<h3>Price Range</h3>
<div>
<input type="checkbox" id="cheap" name="cheap">
<label for="cheap">$200 - $299</label>
</div>
<div>
<input type="checkbox" id="medium-priced" name="medium-priced">
<label for="medium-priced">$300 - $399</label>
</div>
<div>
<input type="checkbox" id="expensive" name="expensive">
<label for="expensive">$400 - $499</label>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>`enter code here`
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 getSnowboardFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.name);
}
});
return opts;
}
function updateSnowboards(opts){
$.ajax({
type: "POST",
url: "submit1.php , submit3.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(data){
$('#snowboards tbody').html(makeTable(data));
}
});
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getSnowboardFilterOptions();
updateSnowboards(opts);
});
updateSnowboards();
</script>
我的submit.php是:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=abc', 'root', '');
$select = 'SELECT *';
$from = ' FROM snowboards';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("male", $opts)){
$where .= " AND gender = 'male'";
}
if (in_array("female", $opts)){
$where .= " AND gender = 'female'";
}
if (in_array("cheap", $opts)){
$where .= " AND price_range > 199 AND price_range < 300";
}
if (in_array("medium-priced", $opts)){
$where .= " AND price_range > 299 AND price_range < 400";
}
if(in_array("expensive", $opts)){
$where .= " AND price_range > 399 AND price_range < 500";
}
if(in_array("xyz", $opts) || in_array("burton", $opts) || in_array("abc", $opts) ||
in_array("www", $opts)){
$brand = " AND (";
if (in_array("xyz", $opts)){
$brand .= " AND brand = 'xyz'";
}
if (in_array("burton", $opts)){
if($brand != " AND (")
$brand .= " OR ";
$brand .= " AND brand = 'burton' ";
}
if(in_array("abc", $opts)){
if($brand != " AND (")
$brand .= " OR ";
$brand .= "AND brand = 'abc'";
}
if(in_array("www", $opts)){
if($brand != " AND (")
$brand .= " OR ";
$brand .= "AND brand = 'www'";
}
$brand .= ")";
$where .= $brand;
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo($json);
?>
提前感谢。问题是,当您选中“男性和女性”复选框时,您的查询将搜索性别等于男性和性别等于女性的记录 尝试替换此:
if (in_array("male", $opts)){
$where .= " AND gender = 'male'";
}
if (in_array("female", $opts)){
$where .= " AND gender = 'female'";
}
为此:
if(!(in_array("male", $opts) && in_array("female", $opts))){
if (in_array("male", $opts)){
$where .= " AND gender = 'male'";
}
if (in_array("female", $opts)){
$where .= " AND gender = 'female'";
}
}
现在,在向where变量添加任何内容之前,我们检查是否同时检查了阳性和阴性
更新:
要解决价格范围的相同问题,请替换:
if (in_array("cheap", $opts)){
$where .= " AND price_range > 199 AND price_range < 300";
}
if (in_array("medium-priced", $opts)){
$where .= " AND price_range > 299 AND price_range < 400";
}
if(in_array("expensive", $opts)){
$where .= " AND price_range > 399 AND price_range < 500";
}
现在查询的结果将是:WHERE。。。和品牌='xyz'或和品牌='burton'
换句话说,您要删除这些行中的所有AND和空格:
$brand .= "brand = 'xyz'";
...
$brand .= "brand = 'burton'";
...
$brand .= "brand = 'abc'";
...
$brand .= "brand = 'www'";
请记住,我在这里输入了这段代码,但还没有测试它,请告诉我是否在函数getSnowboardFilterOptions中出错,尝试在控制台中打印$复选框,并查看它是否返回对象。嘿,谢谢,这工作正常。但我还有一个问题。在“价格范围”中,当我选中这两个复选框时,它将显示“无”。因此,你能在这方面帮助我吗@KoenI已经更新了我的答案,如果你对我的解决方案有任何疑问,一定要问!非常感谢你。给我如此频繁的答复@KoenSorry,我不懂过滤。所以,打扰你了。但是我很困惑。如果我想做多个复选框,那么你的代码是不工作的。因为我有3个价格范围的复选框。如果我添加了新的价格范围,那么它将不起作用。所以,您可以给我一些代码片段的多重复选框@KoenDid只有这三个选项能用吗?如果是这样,我将添加一些注释来解释如何添加另一个复选框
$brand .= " AND brand = 'xyz'";
...
$brand .= " AND brand = 'burton' ";
...
$brand .= "AND brand = 'abc'";
...
$brand .= "AND brand = 'www'";
$brand .= "brand = 'xyz'";
...
$brand .= "brand = 'burton'";
...
$brand .= "brand = 'abc'";
...
$brand .= "brand = 'www'";