Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

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

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

 Share

×
×
  • Create New...