Jump to content

[SOLVED] Custom cart - query


waynew

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.