简体   繁体   English

PHP-购物车,会话问题

[英]PHP - Shopping cart, Session issue

When an item is added to the cart the item id and the quantity of that item is supposed to be displayed. 将商品添加到购物车时,应显示该商品的ID和数量。 In this case only the quantity is being parsed from the session. 在这种情况下,仅从会话中解析数量。 the item ID is not being displayed. 没有显示商品ID。 Also when a different item is added the cart should display a second item with a separate quantity. 同样,当添加其他项目时,购物车应显示另一个具有单独数量的项目。

<?php 
session_start();


?>
<?php 
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;

    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
        $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1));
    } else {
        foreach ($_SESSION["cart_array"] as $each_item) {
            $i++;
            while (list($key, $value) = each($each_item)) {
                if ($key == "item_id" && $value == $pid) {
                array_splice($_SESSION["cart_array"], $i-1, 1,array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                $wasFound = true;
               }
            }
        }
        if ($wasFound == false) {
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    }
  }
 }
?>
<?php
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cart_array"]);
}
?>
<?php
$cartOutput = "";
if (!isset($_SESSION["cart_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++;
        $cartOutput .= "<h2>Cart item $i</h2>";
        while (list($key, $value) = each ($each_item)) {
            $cartOutput .= "key:$value<br />";
        }
    }
}
?>

any suggestions? 有什么建议么? Thanks 谢谢

That's some pretty weird array manipulation you have there. 那是您在其中进行的一些非常奇怪的数组操作。 You should be able to do everything you need with one foreach loop: 您应该可以通过一个foreach循环来完成所需的一切:

    foreach ($_SESSION["cart_array"] as $item_key => $each_item) {
        if ( $each_item['item_id'] == $pid )
        {
            $_SESSION["cart_array"][$item_key]['quantity']++;
        }
        $wasFound = true;
    }

In case it's not familiar to you, foreach ( $foo as $key => $value ) is the normal way of doing what you have written as while( list($key, $value) = each($foo)) and $foo++ is short-hand for $foo = $foo + 1 . 如果您不熟悉它, foreach ( $foo as $key => $value )是您编写while( list($key, $value) = each($foo))$foo++的常规方法$foo++$foo = $foo + 1简写。

You can actually make this even simpler if you use the product IDs as the keys of the shopping cart array; 如果您将产品ID用作购物车数组的键,则实际上可以使此操作更加简单。 that way you can just ask PHP if the key exists, like so: 这样,您可以只询问PHP密钥是否存在,例如:

if ( array_key_exists($pid, $_SESSION['cart_array']) )
{
    $_SESSION['cart_array'][$pid]['quantity']++;
}
else
{
    $_SESSION['cart_array'][$pid] = array('item_id' => $pid, 'quantity' => 1);
}

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

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