Hi all, having a bit of a problem. I’m building an online shop, I’ve now got to the stage where I need to build the shopping cart system. I’m adding the products to the basket using session but I seem to be having a problem, they’re being added but not quite as I like! Please see my code below:
$_SESSION['cart'][]['prod'] = $_POST['prodName'];
$_SESSION['cart'][]['id'] = $_POST['prodId'];
foreach($_SESSION['cart'] as $id=>$value):
echo "You have added ".$value['prod']." which is product ref!".$value['id']."<br/>";
endforeach;
This is the output when I print the array $_SESSION, I have various products added at the moment:
Hey guys, got a small problem, my $_SESSION is only accepting two products, if I had another then it just replaced the second array. Please see my code below:
class cart {
function addItems(){
$i=0;
$_SESSION['cart'][$i]['prod'] = $_POST['prodName'];
$_SESSION['cart'][$i]['id'] = $_POST['prodId'];
$i++;
//print_r($_SESSION);
//session_destroy();
}
}
I can see what I’m doing wrong, each time I call the method it starting back at 0 - not sure of a way around this! Really appreciate any help
<?php
foreach($cart as $id => $product){
printf(
'You have %d of (%d)%s at %f each' . PHP_EOL,
$product['quantity'],
$id,
$product['name'],
$product['price']
);
}
/*
You have 56 of (1)Foo Biscuits at 1.990000 each
You have 3 of (2)Bar Sandwich at 4.990000 each
*/
Hey dude, no I guess it’s not really, ok think I’ll drop that bit, it’s kind of pointless…late night, maybe that’s why I’m being a bit of a noob! Cheers bud