求php增加购物车数量的加减,

购物车代码已给出,求增加数量加减方案代码

伪代码:
前端:在你页面上数量那栏增加

<div class="quantity-form"><a href="javascript:void(0);" clstag="cart_num_down" class="decrement disabled" id="decrement_8888_526830_1_1">-</a>
<input autocomplete="off" type="text" class="itxt" value="1" id="changeQuantity_8888_526830_1_1_0" minnum="1">
<a href="javascript:void(0);" clstag="cart_num_up" class="increment" id="increment_8888_526830_1_1_0">+</a>
</div>
你的购物车是个循环列表,可以去得到,每个商品的信息:id是商品
function cart_num_up(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:-1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){

});
});

}
function cart_num_down(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){

});
});
}
点一次 加或减按钮,触发js向后端发起ajax请求:返回的是增加成功和失败状态和剩余数量;
CART_num.PHP
$id = (int)$_POST['id'];
$num = (int)$_POST['num'];
$uid =(int)$_POST['uid']; //有封装获取post或get函数更好
//查库看库存
$sort = get_kucun_num();
//查看购物车目前数量
$cart_num = get_cart_num(uid,id);
//判断库存
if($sort >$cart_num){
}else{
}

if($num<0){
//减法 $sql =“update cart set cart_num = cart_num-1 where uid =uid and id =id ”
if(($cart_num-1)>1){
}else{
}){
//加法 $sql =“update cart set cart_num = cart_num+1 where uid =uid and id =id ”
}else{
//不正确的请求
}追问

你的购物车是个循环列表,可以去得到,每个商品的信息:id是商品
下面的代码怎么处理,放到哪里

温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-07-10
要实现购物车中商品数量的增加和减少功能,可以通过以下示例代码来实现:
```php
<?php
session_start();
// 初始化购物车
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
// 商品ID
$productId = 123;
// 检查购物车中是否已存在该商品
if (isset($_SESSION['cart'][$productId])) {
// 商品已存在,增加数量
if (isset($_POST['increase'])) {
$_SESSION['cart'][$productId]['quantity']++;
}
// 商品已存在,减少数量
if (isset($_POST['decrease'])) {
$_SESSION['cart'][$productId]['quantity']--;
// 如果数量减少到0,则从购物车中移除该商品
if ($_SESSION['cart'][$productId]['quantity'] <= 0) {
unset($_SESSION['cart'][$productId]);
}
}
} else {
// 商品不存在,添加到购物车
$_SESSION['cart'][$productId] = [
'quantity' => 1,
'name' => '商品名称',
'price' => 9.99
// 其他商品信息
];
}
// 输出购物车中商品数量
echo "购物车数量:" . (isset($_SESSION['cart'][$productId]) ? $_SESSION['cart'][$productId]['quantity'] : 0);
?>
```
在上述示例代码中,我们使用了PHP的`$_SESSION`来存储购物车数据。首先,我们初始化购物车(如果未初始化),然后指定商品ID(这里使用`$productId = 123;`作为示例)。然后,我们检查购物车中是否已存在该商品。
如果商品已存在,则根据用户的操作进行增加或减少数量的操作。当用户点击"增加数量"按钮时(例如通过表单的提交),我们将商品的数量增加1。当用户点击"减少数量"按钮时,我们将商品的数量减少1,并在数量减少到0时从购物车中移除该商品。
如果商品不存在于购物车中,则将其添加到购物车,并设置初始数量为1。
最后,我们输出购物车中指定商品的数量,以便展示给用户。你可以根据实际情况自行修改商品ID、商品信息和购物车展示的方式。
第2个回答  2023-04-02
以下是一个示例代码,用于实现购物车数量的加减功能:
php
<?phpsession_start(); // 启动session// 初始化购物车数量if(!isset($_SESSION['cart_qty'])) { $_SESSION['cart_qty'] = 0;
}// 处理加入购物车的操作if(isset($_POST['add_cart'])) { $qty = $_POST['qty']; // 获取用户选择的数量
$_SESSION['cart_qty'] += $qty; // 增加购物车数量}// 处理从购物车中减少商品的操作if(isset($_POST['remove_cart'])) { $qty = $_POST['qty']; // 获取用户选择的数量
if($_SESSION['cart_qty'] >= $qty) { $_SESSION['cart_qty'] -= $qty; // 减少购物车数量
}
}// 输出购物车数量echo "购物车数量:".$_SESSION['cart_qty'];?><!-- 增加购物车数量的表单 -->
<form method="post">
<input type="hidden" name="qty" value="1">
<button type="submit" name="add_cart">加入购物车</button>
</form>

<!-- 减少购物车数量的表单 -->
<form method="post">
<input type="hidden" name="qty" value="1">
<button type="submit" name="remove_cart">从购物车中移除</button>
</form>

在上面的示例代码中,使用了session来存储购物车数量,用户点击“加入购物车”按钮时,会将用户选择的数量加上购物车数量;用户点击“从购物车中移除”按钮时,会将用户选择的数量从购物车数量中减去。最后,输出购物车数量的值。
相似回答