我正在建立一个过滤系统,我有三个表:
产品:ProductId,CatId,SubCatId,ProductName,ProductDescription
过滤器:FilterId,FilterName
ProductsData :ProductDataId,ProductId,FilterId,ProductDataEN(包含过滤器的值,在我的情况下为" on" 意味着为此特定产品选择了过滤器)
我有过滤信息的脚本,但问题是,sript现在只从ProductsData获取数据,返回的结果是JSON,其中包含此表(ProductsData)中包含每个产品的过滤器值的所有信息。请记住,一个产品可以有多个过滤器,因此ProductsData与产品不匹配 我发布了过滤的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);
答案 0 :(得分:0)
Loop through checkboxes and count each one checked or unchecked 功能更新(检查){ $就({ var info =&#39; id =&#39; + checkOptions; 键入:&#34; POST&#34;, dataType:&#34; json&#34;, url:&#39; submit.php&#39;, 数据:信息, 成功:功能(记录){ $(&#39; .subcategories&#39;)HTML(makeItem(记录))。 } })
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 ]
}
}
}
希望这会有所帮助。