我试图制作一个用户可以购买商品的迷你购物车。 一切正常,但是当我退房时,只有购物车中的第二个产品被装入退房选项而不是第一个产品,这意味着如果购物车只有一个产品,它运作良好,但是当添加第二个产品,它需要第二个产品,而不是第一个产品。请纠正我错误的地方。
这是我在我的关卡中尝试过的代码。
<?php
// Session Start
session_start();
include 'storescripts/dbconnect.php';
?>
<?php
// Add To The Cart
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(
0 => 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));
}
}
header("Location: cart.php");
exit();
}
?>
<?php
// Empty The Cart
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
// Adjust The Item Quantity
if (isset($_POST['itemToAdjust']) && $_POST['itemToAdjust'] != "") {
$itemToAdjust = $_POST['itemToAdjust'];
$itemQuantity = $_POST['itemQuantity'];
$itemQuantity = preg_replace('#[^0-9]#i', '', $itemQuantity);
if ($itemQuantity >= 100) {
$itemQuantity = 99;
}
if ($itemQuantity < 1) {
$itemQuantity = 1;
}
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $itemToAdjust) {
array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $itemToAdjust, "quantity" => $itemQuantity)));
}
}
}
}
?>
<?php
// Remove Item If User Choose To
if (isset($_POST['idToRemove']) && $_POST['idToRemove'] != "") {
$keyToRemove = $_POST['idToRemove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$keyToRemove"]);
sort($_SESSION["cart_array"]);
}
}
?>
<?php
// Render The Cart For The User To View
if (!isset($_SESSION['product_name'])) {
$_SESSION['product_name'] = array();
}
if (!isset($_SESSION['price'])) {
$_SESSION['price'] = array();
}
include 'moneyFormat.php';
$cartOutput = "";
$cartTotal = "";
$cartTotalPrice = "";
$cartTotalPriceString = "";
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) {
$item_id = $each_item['item_id'];
$sql = "SELECT *
FROM products
WHERE id='".$item_id."'
LIMIT 1";
$res = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($res)) {
$productName = $row['product_name'];
//$_SESSION["product_name"] = $productName;
$price = $row['price'];
$details = $row['details'];
}
$priceTotal = $price * $each_item['quantity'];
$cartTotal += $priceTotal;
//echo $_SESSION["product_name"] . "<br />";
for ($i=0; $i < count($_SESSION["product_name"]); $i++) {
echo $_SESSION["product_name"]."<br />";
}
/*if (count($_SESSION["product_name"]) > 0) {
echo $_SESSION["product_name"] . "<br />";
}*/
//setlocale(LC_MONETARY, "en_IN");
$priceTotal = money_format("%10.2n", $priceTotal);
$price = money_format("%10.2n", $price);
$cartOutput .= '<tr>';
$cartOutput .= '<td><a href="product.php?id='.$item_id.'">'.$productName.'</a><br />
<img src="inventory_images/'.$item_id.'.jpg" alt='.$productName.' width="82" height="70" border="1">
</td>';
$cartOutput .= '<td>'.$details.'</td>';
$cartOutput .= '<td style="text-align: center;"> '.$price.'</td>';
$cartOutput .= '<td style="text-align: center;">
<form action="cart.php" method="POST">
<input type="text" name="itemQuantity" size="1" maxlenght="2" value="'.$each_item['quantity'].'"/>
<input type="submit" name="btnAdjustQuantity'.$item_id.'" value="Change" />
<input type="hidden" name="itemToAdjust" value="'.$item_id.'" />
</form>
</td>';
//$cartOutput .= '<td style="text-align: center;">'.$each_item['quantity'].'</td>';
$cartOutput .= '<td style="text-align: center;"> '.$priceTotal.'</td>';
$cartOutput .= '<td style="text-align: center;">
<form action="cart.php" method="POST">
<input type="submit" name="btnDeleteQuantity'.$item_id.'" value="Remove" />
<input type="hidden" name="idToRemove" value="'.$i.'" />
</form>
</td>';
$cartOutput .= '</tr>';
$i++;
}
$cartTotalPrice = money_format("%10.2n", $cartTotal);
$cartTotalPriceString = "<div align='right' style='font-size: 19px; margin-top: 15px;'>Cart Total: ". $cartTotalPrice . "</div>";
}
?>
<?php
$totalItems = 0;
$checkOut = "";
$quantity = 0;
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your Shopping Cart Is Empty.</h2>";
} else {
foreach ($_SESSION["cart_array"] as $each_item) {
while (list($key, $value) = each($each_item)) {
if ($key == 'quantity' && $each_item['quantity'] > 0) {
$totalItems += $value;
$quantity = $totalItems;
}
}
}
}
if ($totalItems > 0) {
$checkOut = '<a href="storescripts/render_cart.php?checkout=YES&Product='.$productName.'&Quantity='.$quantity.'&Price='.$cartTotalPrice.'">';
$checkOut .= '<input type="submit" value="Check Out" name="checkOut" style="float: right;"/>';
$checkOut .= '</a>';
}
?>
感谢。