0

我已经对我的购物车页面进行了编码,但它似乎没有显示任何内容。以前在我的产品页面上,我有一个表单操作,点击添加到购物车后会引导我进入 cart.php。我不确定这里出了什么问题。

<?php
session_start();
//script error reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect.php";
?>

<?php
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// if the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    // run if the cart is empty or not set
    $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1));
} else {
    // run if the cart has at least one item in it
    foreach ($_SESSION["cart_array"] as $each_item) {
        $i++;
        while (list($key, $value) = each($each_item)) {
            if ($key == "item_id" && $value == $pid) {
                //that item is in cart already so let's adjust its quantity using array_slice()
                array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                $wasFound = true;
            } //close if condition
        }//close while loop
    }//close foreach loop
    if ($wasFound == false) {
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    }
  }
}
?>
<?php
 // SECTION 2(if user chooses to empty their shopping cart)
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
// SECTION 3 (render the cart for the user to view)
$cartOutput = "";
if (!isset($_SESSION["crt_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++;
    $item_id = $each_item["item_id"];
    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)) {
        $product_name = $row["product_name"];
        $price = $row["price"];
    }
    $cartOutput .= "<h2>Cart Item $i</h2>";
    //while(list($key, $value) = each($each_item))
          //  $cartOutput .= "$key: $value<br/>";
    $cartOutput .= "Item ID:" . $each_item['item_id'] . "<br/>";
    $cartOutput .= "Item Quantity:" . $each_item['quantity'] . "<br/>";
    $cartOutput .= "Item Name:" . $product_name . "<br/>";
    $cartOutput .= "Item Price: $" . $price . "<br/>";
    }
}
?>
4

1 回答 1

0

你有一个小错误。拼写错误亲爱的。它发生了。人为错误总是发生(:及其在会话购物车数组(:

于 2013-04-14T00:06:26.313 回答