简体   繁体   English

购物车页面未显示任何内容

[英]cart page is not showing anything

I've coded out my cart page and it seems that it is not showing anything. 我已将购物车页面编码,似乎没有显示任何内容。 Previously on my product page, I have a form action that will lead me to cart.php upon clicking add to cart. 以前在我的产品页面上,我有一个表单操作,单击添加到购物车后将引导我到cart.php。 I am not sure what went wrong here. 我不确定这里出了什么问题。

<?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/>";
    }
}
?>

You have a minor error there. 您那里有一个小错误。 Spelling mistake my dear. 亲爱的,拼写错误。 It happens. 它发生了。 Human error always happens(: And its at session cart array(: 人为错误总是会发生(:及其在会话购物车数组(:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM