我正在编写一段代码,但我无法让它工作。
我有这些复选框,当我检查它们时,列表会被过滤,屏幕会通过AJAX进行实时更新。多重过滤有效,但仅在信息仍在屏幕上时才有效。一旦它被过滤掉,它就不会再回来了。我有很多这样的陈述。我是否需要添加If / Else语句?
<?php
$pdo = new PDO('mysql:host=localhost;dbname=records', 'root', '*******');
$select = 'SELECT *';
$from = ' FROM overboekingen';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("coulance_happyhome1", $opts)){
$where .= " AND coulance_happyhome = 1";
}
if (in_array("coulance_happyhome2", $opts)){
$where .= " AND coulance_happyhome = 2";
}
if (in_array("coulance_happyhome3", $opts)){
$where .= " AND coulance_happyhome = 3";
}
if (in_array("coulance_happyhome4", $opts)){
$where .= " AND coulance_happyhome = 4";
}
if (in_array("coulance_happyhome5", $opts)){
$where .= " AND coulance_happyhome = 5";
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>
</td>
<td class="tdfilter">
<label for="coulance_happyhome"><h4>Coulance Happyhome</h4></label>
<input type="checkbox" id="coulance_happyhome" name="coulance_happyhome1">Coulance Agent<br>
<input type="checkbox" id="coulance_happyhome" name="coulance_happyhome2">Coulance Camping<br>
<input type="checkbox" id="coulance_happyhome" name="coulance_happyhome3">Coulance Park<br>
<input type="checkbox" id="coulance_happyhome" name="coulance_happyhome4">Coulance Privatowner<br>
<input type="checkbox" id="coulance_happyhome" name="coulance_happyhome5">Coulance HappyHome<br>
</td>
<td class="tdfilter">
<label for="opmerking"><h4>Opmerking</h4></label>
<input type="checkbox" id="opmerking" name="opmerking">
</td>
</tr>
</table>
<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){
$('#employees tbody').html(makeTable(records));
}
});
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getEmployeeFilterOptions();
updateEmployees(opts);
});
updateEmployees();
答案 0 :(得分:0)
您的代码应如下所示:
if (in_array("coulance_happyhome1", $opts)){
$where .= " OR coulance_happyhome = 1";
}
if (in_array("coulance_happyhome2", $opts)){
$where .= " OR coulance_happyhome = 2";
}
if (in_array("coulance_happyhome3", $opts)){
$where .= " OR coulance_happyhome = 3";
}
if (in_array("coulance_happyhome4", $opts)){
$where .= " OR coulance_happyhome = 4";
}
if (in_array("coulance_happyhome5", $opts)){
$where .= " OR coulance_happyhome = 5";
}
答案 1 :(得分:0)
我认为你的opts数组正在积累但是一旦添加了元素就永远不会丢失。更改更改功能的复选框,如下所示:
$checkboxes.on("change", function(){
opts.length = 0;
opts = getEmployeeFilterOptions();
updateEmployees(opts);
});