【问题标题】:PHP SESSION based shopping cart基于 PHP SESSION 的购物车
【发布时间】:2013-02-21 21:51:57
【问题描述】:

我正在使用 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

不添加第一项,但添加后续项。

不确定为什么第一项没有进入数组?

【问题讨论】:

  • 正在调用 session_start()?
  • 为什么不将产品 ID 作为 $_SESSION['cart'] 的元素?
  • 您究竟是如何检查里面的内容的? print_r($_SESSION)?
  • 使用 Quasdunk idea to print_r 显示元素在其中。问题是我在迭代期间没有得到它

标签: php session shopping-cart shopping


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-05-16
    • 1970-01-01
    • 2011-04-22
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2012-04-10
    • 2013-02-11
    相关资源
    最近更新 更多