I am trying to add item to cart.when I added same item to cart it showing as new item in cart..some errors in my loop.can any one help me to find the issue?
if(!empty($_POST["pro_Count"])) {
if(empty($_SESSION["cart_item"])) {
$_SESSION["cart_item"]=array();
}
$productid= mysqli_real_escape_string($connection,$_POST['pro_Id']);
$sql = "SELECT * FROM $tableposts where id =$productid";
$result = mysqli_query($connection, $sql);
$row= mysqli_fetch_assoc($result);
// $rowCart[]=mysqli_fetch_assoc($result);
// $itemArray = array($row['id']=>array('id'=>$row['id'],'name'=>$row['title'],'quantity'=>$_POST["pro_Count"], 'price'=>$row['saleprice']));
$itemArray = array($row['id']=>array('id'=>$row['id'],'name'=>$row['title'],'quantity'=>$_POST["pro_Count"], 'price'=>$row['saleprice']));
if(!empty($_SESSION["cart_item"])) {
$cartCodeArray = array_keys($_SESSION["cart_item"]);
if(in_array($row['id'],$cartCodeArray)) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($row['id']== $k){
$_SESSION["cart_item"][$k]["quantity"] = $_SESSION["cart_item"][$k]["quantity"]+$_POST["pro_Count"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
// $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
Seems like a plenty much of copy/paste mess;-)
so i wont go into detail, what to do in your code (because: just delete and rewrite it).
How I would do it in short:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.