id未在我的购物车中定义

时间:2014-01-13 09:12:31

标签: php

我正在做购物车教程,

我遇到一个错误,指出我的item_id未定义 $cartOutput .= "Item ID: " . $each_item['item_id']."<br>"; 然而, 我已经定义了item_id。

这是我的代码。 有人能告诉我什么是错的吗?

<?php
//script error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
?>
<?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["supermarketcart"]) || count($_SESSION["supermarketcart"]) < 1) { 
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["supermarketcart"][] = array(1 => array("item_id" => $pid, "quantity" => 1));

    } else {
        // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
        foreach ($_SESSION["supermarketcart"] 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_splice()
                      array_splice($_SESSION["supermarketcart"], $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["supermarketcart"], array("item_id" => $pid, "quantity" => 1));
           }
    }
    header("location: cart.php"); 
    exit();
}
?>
<?php
//if user choose to empty cart
if(isset($_GET['cmd']) && $_GET['cmd'] == "emptycart")
{
    unset($_SESSION["supermarketcart"]);
}
?>

<?php
//render the cart for user to view
$cartOutput = "";
if(!isset($_SESSION["supermarketcart"]) || count($_SESSION["supermarketcart"]) < 1 ){
    $cartOutput .= "<h2 align = 'center'> Your shopping cart is empty</h2>";
}
else
{
    $i = 0;
    foreach ($_SESSION["supermarketcart"] as $each_item)
    {
        $i++;

    $item_id = $each_item['item_id'];

        $sql = mysql_query("SELECT * FROM supermarket WHERE id = '$item_id' LIMIT 1");
        while($row = mysql_fetch_array($sql)){
            $product_des = $row_supermarketDetails['description'];
    $price = $row_supermarketDetails['price'];

    }
        $cartOutput .= "<h2>Cart Item $i</h2>";
        $cartOutput .= "Item ID: " . $each_item['item_id']."<br>";
        $cartOutput .= "Item Quatity: " . $each_item['quantity']."<br>";
        $cartOutput .= "Item Name: " . $product_des."<br>";

        $cartOutput .= "Item Price: " . $price."<br>";



    }
}

?>

1 个答案:

答案 0 :(得分:0)

您的数组结构很复杂,并且您有一个像sp

这样的多维数组
array (
array (
"item_id" => $ pid, "quantity" => 1
)
)

所以你需要调整你的代码

 if (!isset($_SESSION["supermarketcart"]) || count($_SESSION["supermarketcart"]) < 1) { 
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["supermarketcart"][] = array(1 => array("item_id" => $pid, "quantity" => 1));
    }

[]添加到$_SESSION["supermarketcart"]语句中,如上所示