我目前正在努力创建购物车。 它在我将数组存储到产品ID时起作用,但我也需要存储数量。购物车存货编号和数量
我有一个函数
public static function addToCart($data) {
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
else {
array_push($_SESSION['cart'], $data['id']);
}
}
我也有一个函数来获取从车
public static function getCart() {
if(count($_SESSION['cart'])>0) {
$ids = "";
foreach($_SESSION['cart'] as $id) {
$ids = $ids . $id . ",";
}
$ids = rtrim($ids, ',');
$q = dibi::fetchAll("SELECT * FROM eshop_products WHERE idProduct IN ({$ids})");
return $q;
} else {
}
}
项目然后我给你函数变量和使用的foreach。
$cart = Cart::getCart();
foreach($cart as $c) {
echo $c['price'];
}
我到处,了解多维数组,但似乎没有为我工作
谢谢。有用。 :) – George