I am doing shopping cart functionality on an ecommerce site via $_SESSION. I added cart items successfully on every click in $_SESSION['cart'] array. but unable to print it correctly. I made an associative array which is being a problem to handle correctly. I dont know how to show cart items properly, tried merge array += $item or anything but nothing is working.
Here is my code ..
//add item to cart
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
$item = array("pid"=>$_POST['pid'], "name"=> "bloom flowers");
array_push($_SESSION['cart'],$item);
echo "Done!";
?>
//displaying cart items
<?php include 'navbar.php';
session_start();
if(isset($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $value){
echo '<h5>'.$value.'</h5>';
}
}
// print_r($_SESSION['cart']);
?>