dongxu7121 2016-07-06 05:44
浏览 71

使用php和ajax过滤mysql的结果

I am making a filtering system and I have three tables:

Products: ProductId, CatId, SubCatId, ProductName, ProductDescription
Filters: FilterId, FilterName
ProductsData: ProductDataId, ProductId, FilterId, ProductDataEN(containing value for the filter which in my case is "on" meaning that the filter is selected for this particular product)

I have script that filters the information, but the problem is that the sript now gets data only from ProductsData and the returned result is JSON with all the information from this table(ProductsData) which contains the filters values for every product. Keep in mind that one product can have more than one filter therefore ProductsData doesn't match Products.
I am posting the js code for the filtering and the php code for the mysql queries.
My qiestion is how to combine all the tables and the end JSON to be list of products with filters(not just filters).

JavaScript Code:

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 code:

$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);
  • 写回答

1条回答 默认 最新

  • doutao8774 2016-07-06 06:20
    关注

    Loop through checkboxes and count each one checked or unchecked function update(checks){ $.ajax({ var info = 'id=' + checkOptions; type: "POST", dataType : "json", url: 'submit.php', data: info, success: function(records){ $('.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 ]
                  }
                }
                }
    

    Hope this will helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码