基于PHP会话的购物车
我正在为购物车使用基于PHP会话的购物车,php,session,shopping-cart,shopping,Php,Session,Shopping Cart,Shopping,我正在为购物车使用会话['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 = ar
会话['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
不会添加第一项,但会添加后续项
不确定为什么第一项没有进入数组?如果$\u GET['product\u id']是value(比如1、2或3),那么为什么不执行以下操作
$_SESSION[‘cart’][]=$product_id;
print_r($_SESSION);
将输出:
数组([cart]=>Array([0]=>1[1]=>2[2]=>3))正在调用session\u start()?为什么不将产品ID作为$\u session['cart']的元素?您如何准确地检查其中的内容?使用
print\r($\u SESSION)
?使用Quasdunk idea打印显示元素在其中。问题是我在迭代过程中并没有得到它