Hi guys,
I've got a problem with a simple shopping cart I am trying to make. I am new to php so sorry if the code seems lame...
here are the two functions isolated around my problem...
PHP Code:
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";
}
}
The cart items are stored in the array $_SESSION['cart'] [][]
$_SESSION['cart'] [x] is where I keep my cart item number
$_SESSION['cart'] [x][0] - the product ID for the cart item number
$_SESSION['cart'] [x][1] - the quantity
the problem is, every time I add 1 item to a cart, for the first time the quantitiy isn't stored, but it is for every single other item after that.
In other words
PHP Code:
$_SESSION['cart'] [0][1]
for some reason refuses to hold a value.
Even if I hard set
PHP Code:
$_SESSION['cart'] [0][1] = 4
then
PHP Code:
echo $_SESSION['cart'] [0][1];
I get a blank value back. It's only for that one single element, everything else works fine :/
What is going on, can anybody shed some light here, I am pulling my hair out. Worst when you're trying to learn this stuff.
Much appreciated!