Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的购物车中未定义id_Php - Fatal编程技术网

Php 我的购物车中未定义id

Php 我的购物车中未定义id,php,Php,我在做购物车教程 我遇到一个错误,说明我的项目\u id未定义 对于$cartOutput.=项目ID:$每个项目['item\u id']。; 然而 我已经定义了项目的id 这是我的密码。 有人能告诉我怎么了吗 <?php //script error reporting error_reporting(E_ALL); ini_set('display_errors','1'); ?> <?php if (isset($_POST['pid'])) { $pid =

我在做购物车教程

我遇到一个错误,说明我的项目\u id未定义 对于$cartOutput.=项目ID:$每个项目['item\u id']。; 然而 我已经定义了项目的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>";



    }
}

?>

您的数组结构很复杂,并且有一个多维数组,如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]语句中,如上图所示

var_dump$每件商品,并检查其中包含的内容。数组'id'=>string'51'length=2'quantity'=>int 1继续会话开始;藏在什么地方,对吧-