gpt4 book ai didi

php - 仅当购物车中有相同颜色和尺寸的产品时,才在 php 中将产品数量添加到购物车

转载 作者:行者123 更新时间:2023-11-29 12:02:43 27 4
gpt4 key购买 nike

我创建了 php ekart 网站只是为了测试目的。我的查询是,当我单击“添加到购物车”时,它应该首先验证是否选择了任何一种颜色和任何一种尺寸,如果都选择了,则将产品添加到购物车,这很好用,但如果我添加相同的产品但颜色不同或尺寸它与已添加的相同产品重叠,并且产品数量变为 1,但如果我选择另一个产品,它效果很好,但当我选择不同的尺寸或颜色时,同样的情况也会发生。由于我是 php 的初学者,所以我不确定我哪里出错了。

产品添加到购物车的 PHP 代码如下:

$sql = "SELECT * FROM allprods WHERE listing = '$listing' and  id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
$row = mysql_fetch_array($data);
if (isset($_SESSION['cart'][$id]))
{
if(isset($_SESSION['cart'][$id][$color]) && isset($_SESSION['cart'][$id][$size]))
{
$_SESSION['cart'][$id]['quantity']++;
echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
}
else
{
$_SESSION['cart'][$id] = array('quantity' = >1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}
else
{
$_SESSION['cart'][$id] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);

echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}

购物车程序代码

<?php
if(isset($_POST['qty']))
{
foreach($_POST['qty'] as $product_id=>$item_qty)
{
$id=(int)$product_id;
$qty=(int)$item_qty;
if($qty==0)
{
unset($_SESSION['cart'][$id]);
}
elseif($qty>0)
{
$_SESSION['cart'][$id]['quantity']=$qty;
}
}
}
else
{
echo "";
}
if(!empty($_SESSION['cart']))
{
require "../link_db.php";
$sql="SELECT * FROM allprods WHERE id IN(";
foreach($_SESSION['cart'] as $id=>$value)
{
$sql.=$id.',';
}
$sql=substr($sql,0,-1).') ORDER BY product_id ASC';
$data=mysql_query($sql);
while($row=mysql_fetch_array($data))
{
?>
Table row and data goes here!!
<?php
}
else
{
?>
<tr>
<th colspan='4' class='noprodavailablewrap'>
There are no products in your cart.
</th>
</tr>
<?php
}
?>

数据库结构如下: Image of database structure Image of database structure

最佳答案

使用此代码

<?php

$sql = "SELECT * FROM allprods WHERE listing = '$listing' and id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
$row = mysql_fetch_array($data);
$index = md5($id.$color.$size);
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']++;
echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
}else{
$_SESSION['cart'][$index] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}

?>

关于php - 仅当购物车中有相同颜色和尺寸的产品时,才在 php 中将产品数量添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095942/

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