Reputation: 89
I have made a simple webshop script and it works fine! The only thing I'm struggling with now is that I want to make a PHP function which counts how many products the user has in their shopping cart.
I store all the data in sessions. Every product has its own session. Product 1 has cart_1, product 2 had cart_2 etc. If the user has 3 times product 1 in his cart the value of cart_1 will be 3. I want to count all values of sessions which start with 'cart_' I Does anyone know how to manage this?
Upvotes: 0
Views: 1036
Reputation: 31637
Create one session per user, not one session per product. When a user adds a product to the shopping cart, save that information in the user's session:
// User would like to order $count items of $product
$_SESSION['shopping_cart'][$product] = $count;
That way, you can count($_SESSION['shopping_cart'])
to determine how many products the user would like to order and array_sum($_SESSION['shopping_cart'])
to determine how many items the user would like to order.
Upvotes: 2