如何取消设置会话项目计数
How to Unset session item count number
请帮忙,我正在练习建立一个电子商务网站,我希望在他们成功订购产品后,购物车图标中的项目计数将恢复为 0 并停止所有会话。假设我订购了 3 件产品,那么购物车的商品标记为 3,完成后 returns 变为 0。它确实 return 到 0,但是当您单击加载到 load_cart.php 的购物车图标时,它仍会加载到我订购的上一个项目。
来自 checkout_action.php >>>>>going to header("location:order_confirmation.php?checkout=success");
<?php session_start();
if ($_GET['checkout'] =='success'){
unset($_SESSION['item_count']);
}
?>
这是我存放购物车的地方[=14=]
<?php session_start();
$id = $_POST['item_id'];
$quantity = $_POST['item_quantity'];
$_SESSION["cart"][$id] = $quantity;
$_SESSION["item_count"] = array_sum($_SESSION["cart"]);
echo "<span class='carttxt glyphicon glyphicon-shopping-cart'> SHOP </span>
<span class='badge'>" . $_SESSION["item_count"] . "</span>";
然后我使用这个 load_cart.php
加载购物车
<?php
session_start();
include("includes/head.php");
include("includes/db_config.php");
include("includes/jsbottom.php");
$data = "<table class='table table-striped table-responsive'>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-Total</th>
<th>Action</th>
</tr>
</thead>
<tbody>";
$grand_total = 0;
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = "SELECT * FROM items where id = '$product_id' ";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$product_name = $row["product_name"];
$product_desc = $row["product_desc"];
$price = $row["price"];
$image = $row["image"];
//For computing the sub total and grand total
$subTotal = $quantity * $price;
$grand_total += $subTotal;
$data .="<tr>
<td>$product_name . <div class='middle'> <img
src='$image' style='height:100px;width:100px;'/> </div> </td>
<td id='price$product_id'> $price</td>
<td><input type='number' class ='form-control'
value = '$quantity' id='quantity$product_id'
onchange='changeNoItems($product_id)' min='1' size='5'></td>
<td id='subTotal$product_id'>$subTotal</td>
<td><button class='btn btn-danger'
onclick='removeFromCart($product_id)'><span class='glyphicon
glyphicon-trash' aria-hidden='true'></span> Remove</button></td>
</tr>";
}
}
}
$data .="</tbody></table>
<hr>
<a href='checkout.php'> <h3 align='right'>Total: ₱ <span
id='grandTotal'>$grand_total </span><br><button class='btn btn-
success btn-lg'><span class='glyphicon glyphicon-ok-sign'></span>
Check Out</button></h3> </a>
<hr>";
echo $data;
?>
好吧,您的代码没有检查 $_SESSION['item_count']
是否为 0。它正在获取 $_SESSION['cart']
的内容。您需要检查 $_SESSION['item_count']
是否为 0 或销毁 $_SESSION['cart']
例如:
<?php session_start();
if ($_GET['checkout'] =='success') {
session_destroy();
}
?>
请帮忙,我正在练习建立一个电子商务网站,我希望在他们成功订购产品后,购物车图标中的项目计数将恢复为 0 并停止所有会话。假设我订购了 3 件产品,那么购物车的商品标记为 3,完成后 returns 变为 0。它确实 return 到 0,但是当您单击加载到 load_cart.php 的购物车图标时,它仍会加载到我订购的上一个项目。
来自 checkout_action.php >>>>>going to header("location:order_confirmation.php?checkout=success");
<?php session_start();
if ($_GET['checkout'] =='success'){
unset($_SESSION['item_count']);
}
?>
这是我存放购物车的地方[=14=]
<?php session_start();
$id = $_POST['item_id'];
$quantity = $_POST['item_quantity'];
$_SESSION["cart"][$id] = $quantity;
$_SESSION["item_count"] = array_sum($_SESSION["cart"]);
echo "<span class='carttxt glyphicon glyphicon-shopping-cart'> SHOP </span>
<span class='badge'>" . $_SESSION["item_count"] . "</span>";
然后我使用这个 load_cart.php
加载购物车 <?php
session_start();
include("includes/head.php");
include("includes/db_config.php");
include("includes/jsbottom.php");
$data = "<table class='table table-striped table-responsive'>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-Total</th>
<th>Action</th>
</tr>
</thead>
<tbody>";
$grand_total = 0;
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = "SELECT * FROM items where id = '$product_id' ";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$product_name = $row["product_name"];
$product_desc = $row["product_desc"];
$price = $row["price"];
$image = $row["image"];
//For computing the sub total and grand total
$subTotal = $quantity * $price;
$grand_total += $subTotal;
$data .="<tr>
<td>$product_name . <div class='middle'> <img
src='$image' style='height:100px;width:100px;'/> </div> </td>
<td id='price$product_id'> $price</td>
<td><input type='number' class ='form-control'
value = '$quantity' id='quantity$product_id'
onchange='changeNoItems($product_id)' min='1' size='5'></td>
<td id='subTotal$product_id'>$subTotal</td>
<td><button class='btn btn-danger'
onclick='removeFromCart($product_id)'><span class='glyphicon
glyphicon-trash' aria-hidden='true'></span> Remove</button></td>
</tr>";
}
}
}
$data .="</tbody></table>
<hr>
<a href='checkout.php'> <h3 align='right'>Total: ₱ <span
id='grandTotal'>$grand_total </span><br><button class='btn btn-
success btn-lg'><span class='glyphicon glyphicon-ok-sign'></span>
Check Out</button></h3> </a>
<hr>";
echo $data;
?>
好吧,您的代码没有检查 $_SESSION['item_count']
是否为 0。它正在获取 $_SESSION['cart']
的内容。您需要检查 $_SESSION['item_count']
是否为 0 或销毁 $_SESSION['cart']
例如:
<?php session_start();
if ($_GET['checkout'] =='success') {
session_destroy();
}
?>