waynew Posted September 2, 2009 Share Posted September 2, 2009 I've a small custom cart. Its schemata looks like this: cart_id INT(11) NOT NULL AUTO_INCREMENT, session_id VARCHAR(30) NOT NULL, item_id INT(11) NOT NULL, date_added DATETIME DEFAULT NULL, quantity INT(11) NOT NULL I have a simple query that displays what the user has in their cart: $select = " SELECT item.*, cart.* FROM item, cart WHERE cart.session_id = '$session_id' AND cart.item_id = item.item_id ORDER BY cart.date_added DESC"; The thing is; I want all of the rows with the same item_id lumped together... So that I added an item called MYITEM twice on separate occasions, that I would be able to show it like so: MYITEM Quantity:2 Instead of it being: MYITEM Quantity:1 MYITEM Quantity:1 Quote Link to comment https://forums.phpfreaks.com/topic/172850-solved-custom-cart-query/ Share on other sites More sharing options...
Psycho Posted September 2, 2009 Share Posted September 2, 2009 Why are you allowing users to add the same item to their cart multiple times? If the user tries to add an item to their cart which already exists in the cart then you should either add the new quantity to the existing quantity (typically seen in most online shopping sites) or provide some error condition. But, the answer to your question is to use a SUM() on the quantity field and do a GROUP BY based upon the item id. Quote Link to comment https://forums.phpfreaks.com/topic/172850-solved-custom-cart-query/#findComment-910995 Share on other sites More sharing options...
waynew Posted September 2, 2009 Author Share Posted September 2, 2009 Why are you allowing users to add the same item to their cart multiple times? If the user tries to add an item to their cart which already exists in the cart then you should either add the new quantity to the existing quantity (typically seen in most online shopping sites) or provide some error condition. But, the answer to your question is to use a SUM() on the quantity field and do a GROUP BY based upon the item id. I tried using a Group by clause but ran into trouble. Not to worry though. I can't believe that I didn't think of just adding it to the existing quantity. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/172850-solved-custom-cart-query/#findComment-910998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.