i am making a shop cart using $_SESSION
, my problem is the first data in the array is being overwritten by the new one can you help me with this?
here's my code:
session_start();
$_SESSION['cart']= array();
if(isset($_POST['addtocart'])){
$newproduct= array(
'code' => $_SESSION['code'],
'color' => $_POST['color'],
'size' =>$_POST['size'],
'quantity' => 1);
$_SESSION['cart'][]= $newproduct;
}
Retrieving the values:
session_start();
foreach ( $_SESSION['cart'] AS $item )
{
echo 'code: ' . $item['code'] . '<br />';
echo 'color: ' . $item['color'];
}