MySQL Question - grouping things

Soldato
Joined
4 Jul 2004
Posts
2,647
Location
aberdeen
Hello,

I have a shopping basket, and each persons cart is stored in a database.

An example one may looks like this:

id [cart item id] - ItemID - Quantity

1 - 2 - 3
2 - 23 - 2
3 - 53 - 1


Each item (that itemID refers to) has a category, so in the item table it is like this (with example data):

id [item id] - title - category_id

2 - "example title" - 1
23 - "Another tiel" - 1
52 - "fubar" - 2
53 - "foobaa" - 3


I want to display the contents of the cart (there are other things, like session id's to help find out whos cart to display, but i'm ignoring that.

At the moment, it does this:
Select cart.itemid, cart.quantity, item.title from cart left join item on item.id = cart.itemid where cart.sessionid = "$usersessionid";

However, i want to display the cart on this page seperated by the groups, which come from the item.category_id field. So, it'll display like this:

CATEGORY NAME
Item 1
Item 2

ANOTHER CATEGORY THAT YOU HAVE ADDED ITEMS TO IN YOUR CART
Item 3
Item 4

etc.

So in a way, it is grouping it (if this is a technical term, i am probably using it in the wrong sense. I use it here, just as i see it - it is grouping it)

Can someone help me on how to do this?

Hope i've made sense.

thanks
 
Hey,
You could try to use ORDER BY category, and in your shopping basket code, as you loop through the record set, check the category name and print if it has changed. Thats easy to do if you are using php or something like that.
 
Back
Top Bottom