Reputation: 153
After a button is being pressed, 3 values are put in into a multidimensional array in PHP. I've checked the values before assigning in the same brackets and it seems to have the correct value. However, when I add the values like this:
if (isset($_POST['add_to_cart'])) {
$count = count($_SESSION['shopping_cart']);
echo "Count: $count<br />";
$_SESSION['shopping_cart'][$count]['product_id'] = $_POST['product_id'];
$_SESSION['shopping_cart'][$count]['tier'] = $_POST['tier'];
$_SESSION['shopping_cart'][$count]['division'] = $_POST['division'];
}
The output shows that the array $_SESSION['shopping_cart']
is empty and has no values.
if (empty($_SESSION['shopping_cart'])) {
echo "Your cart is empty.<br />";
}
else {
//Display products in cart
foreach($_SESSION['shopping_cart'] as $id => $product) {
echo $product['tier'] . $product['division'] . "<br />";
}
}
I've came to the conclusion that I assign the values in a wrong way. What am I doing wrong? Thanks for the help!
EDIT: forgot to add that the array is already initialized at the beginning!
if(!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = array();
}
Upvotes: 1
Views: 64
Reputation: 153
I forgot to add the session_start();
to the code. That's why the Session array didn't work! Thanks for the help @Will
Upvotes: 3