Hey guys, just an early warning I am a newbie to php
I am trying to make a simple shopping cart but I’ve a problem and have spent hours trying to figure it out…
first, I am storing my cart items in a multi dimension session array like this
$_SESSION[‘cart’] [x] = cart item number
$_SESSION[‘cart’] [x] [0] = product ID number
$_SESSION[‘cart’] [x] [1] = quantity
I’ve created these two functions to add items to the array…
[php]
function itemsincart() //returns total items in cart
{
$i=0;
while(isset($_SESSION[‘cart’][$i][0])) { $i++; }
return ($i);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
function addtocart() // add the product to the cart using the product id.
{
if(isset($_POST[‘addproductid’])) // new products waiting to be added to cart from addtocart form
{
$_SESSION['cart'][itemsincart()][0] = $_POST['addproductid']; // add a new product onto the end of the array
$_SESSION['cart'][itemsincart()][1] = $_POST['productquantity']; // quantity of new product
unset($_POST['addproductid']);
unset($_POST['productquantity']);
echo $_SESSION['cart'][itemsincart()][1]." * Product ".$_SESSION['cart'][itemsincart()][0]." added to cart";
}
}
[/php]
everything works fine but the problem is , for the very first item you add it doesn’t store the quantity.
It just seems to be that single part of the array which will not hold any values at all.
Even if I hard set it too say $_SESSION[‘cart’][0][1] = 4; and echo it back, it just returns a blank
Can anyone see what I am doing here.