【问题标题】:Filtering results from mysql with php and ajax使用 php 和 ajax 从 mysql 过滤结果
【发布时间】:2016-11-08 02:12:29
【问题描述】:

我正在做一个过滤系统,我有三个表:

产品:ProductId、CatId、SubCatId、ProductName、ProductDescription
过滤器:FilterId、FilterName
ProductsData:ProductDataId、 ProductId,FilterId,ProductDataEN(包含过滤器的值,在我的例子中是“on” 表示为该特定产品选择了过滤器)

我有过滤信息的脚本,但问题是 sript 现在仅从 ProductsData 获取数据,返回的结果是 JSON,其中包含来自该表 (ProductsData) 的所有信息,其中包含每个产品的过滤器值。请记住,一种产品可以有多个过滤器,因此 ProductsData 与 Products 不匹配。
我正在发布用于过滤的 js 代码和用于 mysql 查询的 php 代码。
我的问题是如何将所有表格和最终 JSON 组合成带有过滤器的产品列表(不仅仅是过滤器)。

JavaScript 代码:

function makeItem(data){
    var tbl_body = "";
    $.each(data, function() {
        var tbl_row = "";
        tbl_row += "<div class='subCatName'>"+this.FilterId+"</div>";
        tbl_row += "<div class='subCatText'>"+this.ProductId+"</div>";
        tbl_body += "<div class='categoryWrap'>"+tbl_row+"</div>";
    })
    return tbl_body;
}

function getChecks(){
    var checks = [];
    $checkboxes.each(function(){
        if (this.checked) {
            checks.push(this.name);
        };
    })
    return checks;
}

function update(checks){
    $.ajax({
        type: "POST",
        dataType : "json",
        url: 'submit.php',
        data: {checkOptions : checks},
        success: function(records){
            $('.subcategories').html(makeItem(records));
        }
    })
}

var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
    var checks = getChecks();
    update(checks);
});

update();

PHP 代码:

$select = "SELECT *";
$from = " FROM ProductsData";
$where = " WHERE TRUE ";
$checks = isset($_POST['checkOptions'])? $_POST['checkOptions'] : array('');

foreach ($filters as $key => $filter) {
  if (in_array($filter['nameBG'], $checks)) {
    $where .= "AND FilterId = $filter[id]";
  }
}
$sql = $select . $from . $where;
$statement = $db -> query($sql);

while( $row = $statement -> fetch_assoc()) {
    $json[] = $row;
}
$json1 = json_encode($json);
echo($json1);

【问题讨论】:

  • 如果是我,我会暂时忘记这里所有非 MySQL 的东西,只关注 SQL。提供正确的 CREATE 和 INSERT 语句以及所需的结果
  • 你试过 echo $sql;在执行它之前。 AND 是 $filters 数组存储您传递的复选框的值吗?
  • 它执行正确,但最终结果不是我需要的。最终结果是来自 ProductsData 的everithyng,但我需要 ProductId、ProductName 以及与此 ProductId 关联的所有 FilterId

标签: javascript php mysql json ajax


【解决方案1】:

Loop through checkboxes and count each one checked or unchecked 功能更新(检查){ $.ajax({ var info = 'id=' + checkOptions; 类型:“发布”, 数据类型:“json”, 网址:'submit.php', 数据:信息, 成功:函数(记录){ $('.subcategories').html(makeItem(records)); } })

            foreach ($filters as $key => $filter) {
              if (in_array($filter['nameBG'], $checks)) {
                $where .= "AND FilterId IN($_REQUEST['id'])";// this id comes from info variable and IN Operator since there will be multiple checkbox value must be [5 or 5,6 this will be checkbox value ]
              }
            }
            }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-10-05
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多