我需要购物车页面显示摘要"订单项"比如在我的售货页面上购买的所有自动售货机的视图都是$ _SESSION [' cart']
这是我的代码,它允许我向购物车添加多个项目:(供应商页面)
session_start();
//Check if $_SESSION['cart] exists
if (!isset($_SESSION['cart'])) {
//initiate cart as empty array
$_SESSION['cart'] = [];
}
if (isset($_POST['add'])) {
//add product id to session cart
$_SESSION['cart'][$_POST['add']] =1;
}
else if (isset($_POST['remove'])) {
//remove product id from session cart
unset($_SESSION['cart'][$_POST['remove']]);
}
我只需要一种方式来显示项目I"添加到购物车"在卖方页面上#34;在列表中显示到另一个页面"购物车页面"。我做了一个图像来展示它应该如何显示:HERE
答案 0 :(得分:0)
类似
if (isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $name => $price){
echo $name . " - " . $price;
}
}
答案 1 :(得分:0)
添加此内容 -
session_start();
if (isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $name => $price){
echo $name . " - " . $price;
}
}else{
// optional(if cart is empty it will redirect to another page)
header("location:products.php");
}