php - 多个复选框过滤器未正确过滤

标签 php mysql ajax json checkbox

我正在编写一段代码,但无法让它工作。

我有这些复选框,当我选中它们时,列表会被过滤,并且屏幕会通过 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();

最佳答案

您的代码应如下所示:

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";
}

关于php - 多个复选框过滤器未正确过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22408071/

相关文章:

php - 查询执行需要时间

php - 基于MySQL表通过PHP创建一条空白记录

php - Yii2:根据相关表中的另一个字段自动填充字段

javascript - 断言失败 : The value that #each loops over must be an Array. 你通过了 [object Object]

javascript - 使 json 字符串与饼图数据源的确切字符串匹配?如何

php - WordPress 动态地在列上划分帖子

php - 向哈希添加盐后无法使登录脚本工作;之前工作得很好

php - 以编程方式替换 PHP 数组中值的表示

php - 如何限制一段时间的mysql_array?

mysql - mysql查询返回受WHERE子句限制的内连接数