我正在尝试用 PHP 和 AJAX 制作一个电子商务过滤系统。我在YouTube上看过一些人,它起作用了,但我的电子商务网站是服装行业,而他的电子商务网站是电子产品......所以他没有解释如何过滤与数量一起存储的尺码(例如:S: 2、M:3、L:4) 配有过滤系统。现在,如果我为产品添加过滤器,该产品会正确显示该尺寸。但如果我第二次尝试,它就不会执行。我想是因为我的专栏在 MySQL 中的设置方式。请帮忙。
这是我的过滤系统代码:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="S" id="Maat_Voorraad">S
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="M" id="Maat_Voorraad">M
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="L" id="Maat_Voorraad">L
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="XL" id="Maat_Voorraad">XL
</label>
</div>
</li>
</ul>
<script type="text/javascript">
$(document).ready(function(){
$(".product_check").click(function(){
$("#loader").show();
var action = 'data';
var type = get_filter_text('type');
var maat_voorraad = get_filter_text('Maat_Voorraad');
$.ajax({
url:'action.php',
method:'POST',
data:{action:action,type:type,maat_voorraad:maat_voorraad},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Gefilterde producten");
}
});
});
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
});
</script>
// action.php
if(isset($_POST['maat_voorraad'])){
$maat_voorraad = implode("','", $_POST['maat_voorraad']);
$sql .= "AND Maat_Voorraad LIKE '%$maat_voorraad%'";
}
// Maat_Voorraad refers to my table column in database. There the size and quantity is
stored per shirt like: S:2, M:4, L:3
// maat_voorraad means size_and_quantity and that variable sends only the size
参见示例here 我非常感谢您的帮助!
最佳答案
从您共享的示例代码中,我可以看到您的数据库设计存在问题。 我可能会有一个存储衬衫信息的表,例如:
- 身份证
- 衬衫名称
- 衬衫价格(最低 SKU 的价格)。
- 时间戳(添加项目时)。您可以添加与产品相关的任意数量的时间戳。
另一个存储衬衫尺寸的表:
- ID
- 姓名(例如“S”)
另一个表(PIVOT 表)包含有关衬衫、尺寸以及相关价格和可用性的信息:
- shirt_id(外键引用衬衫表)
- size_id(外键引用衬衫尺码表)
- unit_price(该衬衫特定尺码的价格)
- discount_price(如果衬衫有特价)
保存每件衬衫图像的表格:
- ID
- shirt_id(外键引用衬衫表)
- url(衬衫图片的位置)
- 时间戳(添加图像的时间以及上次更新图像的时间)。
最后是订单表
- id(订单 ID)
- shirt_id(外键引用衬衫表)
- shirt_size_id(外键引用衬衫尺码表)
- 数量
- 总计
- 日期(订单创建、订单履行)
这只是您可以用来更有效地设计数据库的指南。我省略了一些列,因为我认为您可能已经有了它们。如果您想在一个订单中实现多个项目,只需为订单项目创建一个表,在其中存储已订购的各个项目,然后再创建一个存储订单总额的表。
下面的链接将更清楚地说明我上面所写的内容。
此时规范化数据库以使查询更容易是安全的。引用Normalization以获得更多理解。
关于php - 如何使用 PHP 和 AJAX 过滤服装尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64776245/