我们有一个网站,企业(用户)上传他们的产品,他们插入品牌、颜色、 Material 等规范。
当访问者想要找到引人注目的产品时,他们可以选择企业(用户)之前插入的复选框,例如品牌、颜色、 Material 。
所有复选框结果均通过 Ajax 显示
function getEmployeeFilterOptions() {
var opts = [];
$checkboxes.each(function () {
if (this.checked) {
opts.push(this.value);
}
});
return opts;
}
function updateEmployees(opts) {
$.ajax({
type: "POST",
url: "ajax/filterProducts.php",
data: {
filterOpts: opts, catIDp2:<?php echo $_GET['catIDp']; ?>
},
success: function (records) {
$('#response').html(records);
}
})
;
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function () {
var opts = getEmployeeFilterOptions();
updateEmployees(opts);
});
现在我们遇到了一个问题,复选框工作不完整,例如我们有 3 个针对移动设备的复选框:
1.品牌:三星、LG、苹果
2.颜色:红、蓝、绿
3. Material :塑料、金属、玻璃
如果访客点击三星(品牌)和红色(颜色)复选框,则必须显示红色三星的结果。
但我们的主要问题是访问者必须单击所有复选框(红色、金属、三星)才能显示结果。
因此,我们只想显示访问者点击的结果,而不是填写所有复选框。
我们的 php 代码在这里:
function getFilterProducts($cat, $filter = null){
global $db;
$result = '';
$sql = '';
if (count($filter) == 1) {
if ($filter != null) {
$val = implode("','", $filter);
$sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C,brandNameFarsi_Product_C,companyID,genreProductFarsi_C,colorProductFarsi_C
FROM $db->products_company WHERE (countryProductFarsi_C IN ('$val') AND subCatID_Product='$cat') OR (brandNameFarsi_Product_C IN ('$val') AND subCatID_Product='$cat') OR (companyID IN ('$val') AND subCatID_Product='$cat') OR (genreProductFarsi_C IN ('$val') AND subCatID_Product='$cat') OR (colorProductFarsi_C IN ('$val') AND subCatID_Product='$cat') ORDER BY create_date_product DESC";
$result = $db->query($sql);
}
} elseif (count($filter) >= 2) {
if ($filter != null) {
$val = implode("','", $filter);
$sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C,brandNameFarsi_Product_C,companyID,genreProductFarsi_C,colorProductFarsi_C
FROM $db->products_company WHERE subCatID_Product='$cat' AND countryProductFarsi_C IN ('$val') AND brandNameFarsi_Product_C IN ('$val') AND companyID IN ('$val') AND genreProductFarsi_C IN ('$val') AND (colorProductFarsi_C IN ('$val')) ORDER BY create_date_product DESC";
$result = $db->query($sql);
}
} else {
$sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C
FROM $db->products_company WHERE subCatID_Product='$cat' ORDER BY create_date_product DESC";
$result = $db->query($sql);
}
if ($result) {
$listFilterPrd = $result->fetch_all(MYSQLI_ASSOC);
return $listFilterPrd;
}
return null;}
最后,我们希望复选框能够像 amazon.com 或 ebay.com 一样正常工作
我们如何解决这个问题?
最佳答案
我认为你应该动态准备查询 例如
谨防 SQL 注入(inject)
注意:我只是在编写伪代码(只是关于如何实现的想法)请根据您的需要进行调整
$sql ="select * from products where 1";
if($filter['color'])
$sql.="and color='$color'";
if($filter['manufracture'])
$sql.="and color='$manufracture'";
....
and lastly execute the query.
关于php过滤mysql多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396633/