kerry Posted August 8, 2011 Share Posted August 8, 2011 I have created the cart from the book (as per the code) and this works great, but on every page I would like for example, "You have 2 items in your cart" which is taking the quantity from the cart. Do you have any code on how this can be achieved on every page? Thanks Link to comment Share on other sites More sharing options...
Jonathon Posted August 8, 2011 Share Posted August 8, 2011 I can't remember the precise code for this example. But I think the data is stored in the $_SESSION. So you can query the $_SESSION to see if it's empty. If it isn't then query the $_SESSION to find out how many items you have and print this to the page. Link to comment Share on other sites More sharing options...
kerry Posted August 8, 2011 Author Share Posted August 8, 2011 I tried this, but no quantities are shown: <?php $_SESSION['cart'][$row['quantity]]; echo "<p>There are {$row['quantity']} items in your basket</p>"; ?> Link to comment Share on other sites More sharing options...
Larry Posted August 10, 2011 Share Posted August 10, 2011 Items are stored in $_SESSION['cart'], with each item being a single element in that array. To get the number of items, you could use count($_SESSION['cart']). However, that wouldn't reflect if there were multiple copies of an item in the cart. To get that, you'd need to loop through the $_SESSION['cart'] array and add up all the individual items. Link to comment Share on other sites More sharing options...
Recommended Posts