提前感谢您的帮助。
我正在尝试制作购物车并且它几乎完成但只有一个问题,就是当我尝试添加第三个产品或者说超过2个产品时它只向我显示2个产品并从列表中删除第一个条目。这是我的购物车代码。
<?php
session_start();
include_once './includes/conn_manager.php';
//empty cart by destroying current session
if (isset($_GET['empty_cart']) && $_GET['empty_cart'] == 1) {
$return_url = base64_decode($_GET['return_url']); //retuen url
unset($_SESSION['cart']);
header("Location: " . $return_url);
}
//add item in cart
if (isset($_POST['type']) && $_POST['type'] == 'add') {
$qtt = filter_input(INPUT_POST, 'qtt', FILTER_DEFAULT); //product quantity
$e1 = explode(" - Rs.:", $qtt);
$quantity = trim($e1[0]);
$price = trim($e1[1]);
$pid = filter_input(INPUT_POST, 'prid', FILTER_DEFAULT); // product id
//$return_url = base64_decode($_POST['return_url']); // return url
$result = $mysqli->query("select * from pro_data where pro_uid='$pid' limit 1");//getting product info from db
$obj = $result->fetch_object(); //fetching product info as array of objects
if($result)// check if $result worked
{
// now we have the product info
// making product info array
$new_product = array(array('pnm'=>$obj->pro_nm,'pcode'=>$pid,'pqtt'=>$quantity,'pprice'=>$price));
if(isset($_SESSION['cart'])){
$isin = false;
foreach ($_SESSION['cart'] as $cart_item){
if($cart_item['pcode'] == $pid){
$products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price));
$isin = true;
} else {
$products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]));
}
}
if($isin == false){
$_SESSION['cart'] = array_merge($products, $new_product);
} else {
$_SESSION['cart'] = $products;
}
} else {
$_SESSION['check'] = 'reached';
$_SESSION['cart'] = $new_product;
}
}
}
?>
答案 0 :(得分:1)
问题在于您生成$ products数组的方式:
<?php
foreach ($_SESSION['cart'] as $cart_item){
if($cart_item['pcode'] == $pid){
$products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price));
$isin = true;
} else {
$products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]));
}
}
生成的$ products将始终是一个只包含一个条目的数组,因为您为foreach的每次迭代都会覆盖$ products变量。
你最好做一些事情,比如:
<?php
$products = array();
foreach ($_SESSION['cart'] as $cart_item){
if($cart_item['pcode'] == $pid){
$products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price);
$isin = true;
} else {
$products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]);
}
}
$ products []语法会在每次迭代时将购物车条目附加到$ products数组的末尾。 通过快速重构(可能仍然更好):
<?php
$new_product = array('pnm'=>$obj->pro_nm,'pcode'=>$pid,'pqtt'=>$quantity,'pprice'=>$price);
if (isset($_SESSION['cart'])) {
$isin = false;
$products = array();
foreach ($_SESSION['cart'] as $cart_item) {
if ($cart_item['pcode'] == $pid) {
$products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price);
$isin = true;
} else {
$products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]);
}
}
// If the new product is not already in the list, we add it
if ($isin == false) {
$products[] = $new_product;
}
$_SESSION['cart'] = $products;
} else {
$_SESSION['check'] = 'reached';
$_SESSION['cart'] = array($new_product);
}