Please i need help, I am creating a shopping cart feature for a cake store. when i add a product it works but if i add it again it shows up as a 2nd array. How can i make sure a product just gets added once. i used in_array() function but i don't seem to get it right.
Here's my code
<?php
session_start();
if(isset($_POST['add_to_cart']))
{
// if the cart isn't opened
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'][0]=array('cakename'=>$_POST['cakename'],'cakeprice'=>$_POST['cakeprice'],'Quantity'=>1);
echo "<script>
alert('Item Added');
window.location.href='index.php';
</script>";
}
// If the product already exists in the shopping cart
elseif(in_array($_POST['cake_name'],$_SESSION['cart']))
{
echo "<script>
alert('Item ALREADY Added');
window.location.href='index.php';
</script>";
}
else
{
$count= count($_SESSION['cart']);
$_SESSION['cart'][$count]= array('cakename'=>$_POST['cakename'],'cakeprice'=>$_POST['cakeprice'],'Quantity'=>1);
echo "<script>
alert('Item Added');
window.location.href='index.php';
</script>";
}
}
?>