カート ページをコーディングしましたが、何も表示されていないようです。以前の製品ページには、カートに追加をクリックすると、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/>";
}
}
?>