基于PHP SESSION的购物车

时间:2013-02-21 21:51:57

标签: php session shopping-cart shopping

我正在使用SESSION['cart']购物车:

if(isset($_GET['product_id'])){

    echo 'Good request!';

    $product_id = $_GET['product_id'];
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
        echo 'There is no cart!';
    }
    $cart_row = array(
        'product_id'=>$product_id
    );

    $_SESSION['cart'][] = $cart_row;
}

当我使用以下内容添加项目时

addToCart.php?product_id=12345

未添加第一项,但后续项目为。

不确定为什么第一项没有进入阵列?

1 个答案:

答案 0 :(得分:0)

如果$ __ GET ['product_id']是值(比如说1,2或3),那么为什么不执行以下操作

 $_SESSION[‘cart’][]=$product_id;
 print_r($_SESSION);

会输出:      数组([cart] =>数组([0] => 1 [1] => 2 [2] => 3))

相关问题