有没有其他方法可以将数组存储到没有重复项的会话变量中?

时间:2019-05-05 17:45:28

标签: php arrays session

我正在尝试开发一个可以在会话中使用的购物车,购物不能重复,因此需要添加的每个商品都是唯一的,否则会警告用户已经添加了商品。

我是通过合并一个数组和一个if语句来检查该变量是否已经存在的方式来执行此操作的,由于某种原因,逻辑似乎仍然允许重复并在某个点处停止。


//We search for the product that has been clicked on
    $sql = $conn->prepare('SELECT * FROM product WHERE product_id= :product_id');
    $sql -> execute(['product_id' => $product_id]); //execute the statement
    $row = $sql->fetch();

    $product_name = $row['product_name'];
    $product_id = $row['product_id'];
    $product_price = $row['product_price'];

    //You could perform another search here to obtain the product image

    $cartArray = array(
        $product_id=>array(
        'product_name'=>$product_name,
        'product_id'=>$product_id,
        'product_price'=>$product_price,
        'product_quantity'=>1
        )
    );

    // we perform some logic that detects if the product is already in the basket.
    // If it is, we display an error message. Increasing quantity is handled on the cart page
    if(empty($_SESSION["shopping_cart"])) {
        $_SESSION["shopping_cart"] = $cartArray;
        $status = "<div class='box'>Product is added to your cart!</div>";
    }else{
        $array_keys = array_keys($_SESSION["shopping_cart"]);

        if(in_array($product_id,$array_keys)) {
        $status = "<div class='box' style='color:red;'>
        Product is already added to your cart!</div>";

        } else {
        $_SESSION["shopping_cart"] = array_merge(
        $_SESSION["shopping_cart"],
        $cartArray
        );
        $status = "<div class='box'>Product is added to your cart!</div>";


    }

}
}


I would like to know if there was anything that I need to do differently to prevent duplication

1 个答案:

答案 0 :(得分:0)

我建议这样做:

//We search for the product that has been clicked on
$sql = $conn->prepare('SELECT * FROM product WHERE product_id= :product_id');
$sql -> execute(['product_id' => $product_id]); //execute the statement
$row = $sql->fetch();   

// Collect from $row just what must be in cart session
foreach ( $row as $index => $value ) {
    if ( in_array( $index, $cartDesign ) ) {
        $product[ $index ] = $value;
    }
}

// Store session in Array
$currentCart = $_SESSION[ 'shopping_cart' ];

// Product properties. Quantity comes after.
$cartDesign = array(
    'product_id',
    'product_name',
    'product_id',
    'product_price',
);

// Checks if products is already in the cart
$inCart = false;
foreach ( $currentCart as $index => $cartItem ) {
    if ( $cartItem[ 'product_id' ] === $product[ 'product_id' ] ) {
        $inCart = true;
        $indexInCart = $index;
    }
}

if( $inCart ) {
    // One more in quantity of prodduct (if desired). Comment it if the product just can be or not be in the cart
    $_SESSION["shopping_cart"][ $indexInCart ][ "quantity" ] = $currentCart[ "shopping_cart" ][ $indexInCart ] + 1;
    // Send status: product is already there
    $status = "<div class='box' style='color:red;'>Product is already added to your cart!</div>";
} else {
    // Send status: product was not there
    $status = "<div class='box'>Product is added to your cart!</div>";
    $array_keys = array_keys($_SESSION["shopping_cart"]);

    // Adding to cart in session
    $appendToCart = array( $product, "quantity" => 1 );
    $_SESSION[ "shopping_cart" ][] = $appendToCart;
}

这种方式需要会话设计$_SESSION[ "shopping_cart" ][ int $index ][ array $product ]$_SESSION[ "shopping_cart" ][ int $index ][ "quantity" ][ int $quantity ]