Thanks for help in advance.
I am trying to make shopping cart and its almost done but only having an issue, that is when i try to add 3rd product or say more than 2 products it only show me 2 products and remove very first entry from the list. Here is my add to cart code.
<?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;
}
}
}
?>