gpt4 book ai didi

php - 如何使用 PHP 和 AJAX 按尺码过滤衣服?

转载 作者:行者123 更新时间:2023-12-04 08:36:24 25 4
gpt4 key购买 nike

我正在尝试使用 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
我真的很感激你的帮助!

最佳答案

从您共享的示例代码中,我可以看到您的数据库设计存在问题。
我可能会有一个表来存储衬衫信息,例如:

  • 编号
  • Jersey 名称
  • 衬衫价格(最低 SKU 的价格)。
  • 时间戳(添加项目时)。您可以添加与产品相关的尽可能多的时间戳。

  • 另一个存储衬衫尺寸的表:
  • id
  • 姓名(例如“S”)

  • 另一个表(PIVOT 表),其中包含有关衬衫的信息、其尺寸以及相关的价格和可用性:
  • shirt_id(外键引用衬衫表)
  • size_id(外键引用衬衫尺码表)
  • unit_price(所述衬衫特定尺寸的价格)
  • 折扣价格(如果衬衫有特价)

  • 一张表来保存每件衬衫的图像:
  • id
  • shirt_id(外键引用衬衫表)
  • url(衬衫图片的位置)
  • 时间戳(添加图像的时间和上次更新的时间)。

  • 最后,订单表
  • id(订单编号)
  • shirt_id(外键引用衬衫表)
  • shirt_size_id(外键引用衬衫尺寸表)
  • 数量
  • 总计
  • 日期(订单已创建,订单已完成)

  • 这只是您可以用来更有效地设计数据库的指南。我省略了一些列,因为我认为您可能已经有了它们。如果您想在一个订单中实现多个项目,只需为订单项目创建一个表,您可以在其中存储已订购的单个项目,然后有一个表来存储订单总数。
    下面的链接将更清楚地说明我上面写的内容。
    在这一点上,规范化您的数据库以使查询更容易是安全的。引用 Normalization为了更多的理解。

    关于php - 如何使用 PHP 和 AJAX 按尺码过滤衣服?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64776245/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com