无法在购物车中显示完美数量。 [英] Unable To Show Perfect Quantity in Cart.
问题描述
您好,
我正在网上开店,用户可以在这里购买产品。在制作时,我收到了错误。
错误是当用户按下添加到购物车上的按钮时,购物车没有显示数量,我的意思是确切的数量,因为当我将1个产品添加到购物车时,它会完美地添加,但是当我添加第二个产品时,它会从1开始,我不知道背后的原因。
这是cart.php页面的源代码=>
Hello,
Am making an online shop where users can buy the products. While making it, am getting an error.
The error is that when the user presses the button on add to cart, the cart doesn't show up with the quantity, I mean the exact quantity, because when I add 1 product to the cart, it adds perfectly but when I add second product, it starts again from 1 and I don't know the reason behind it.
Here's the source code of the cart.php page =>
<?php
session_start();
?>
<?php
if (isset($_POST['pid'])) {
$id = $_POST['pid'];
$wasFound = false;
$i = 0;
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$_SESSION["cart_array"] = array(
1 => array(
"item_id" => $id, "quantity" => 1
)
);
} else {
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $id) {
array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $id, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
}
}
}
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $id, "quantity" => 1));
}
}
}
?>
<?php
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
// Render The Cart For The User To View
$cartOutput = "";
$total_items = 0;
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your Shopping Cart Is Empty.</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
$cartOutput .= "<h2>Cart Item " . $i . "</h2>";
while (list($key, $value) = each($each_item)) {
$cartOutput .= $key . " : " . $value . "<br />";
}
}
}
?>
请指导我在哪里犯错,以便我能纠正它。
谢谢。
MaddyDesigner。
Kindly guide me where I am making the mistake so that I can rectify it.
Thanks.
MaddyDesigner.
推荐答案
_POST [' pid'])){
_POST['pid'])) {
id =
id =
_POST [' pid'];
_POST['pid'];
这篇关于无法在购物车中显示完美数量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!