I am currently creating a custom e-commerce site (in php, but that’s not really relevant for this question).
I have just got to creating the shopping basket, and cannot decide between the following 2 options:
option 1:
Basket table:
- id
- user
- items
In this option, I would have one row per user, with all of the items and quantities stored in the items field.
This format is already used in the cookie based basket for non-logged in users, so parsing of the items field is no problem.
option 2:
Basket_items table:
- id
- user
- item
- quantity
In this option, I would have one row per item in the basket.
option 3:
suggest a better idea.
conclusion
Both of these options are equally easy for me to implement, so the question comes down which would be more efficient/convenient for updating the basket.
Thank you for any answers, Nico
Option 2 is the way to go. Storing all items and quantities in items field (option 1) means you are going against the relational nature of MySQL. You’ll have to define a format and parse it with option 1, additional code you don’t have write with option 2. Also, with Option 2, you’ll be able to do other things easier down the line, like calculate totals, shipping amounts, etc, as well as reporting on item quanities sold (just a simple query).
Of course, if I was writing this, I’d also ask myself if there is a library available to do this – why reinvent such common a functionality as shopping cart. I am not from PHP world, so I don’t know what the options are, but I am sure there must be something you can reuse. So ultimately, I’d encourage you to choose option 3 – don’t implement it yourself if you an avoid it 🙂