简体   繁体   中英

How to store values in the database for a complex shopping cart

I'm working on a shopping cart with a little bit complex way of storing products in cart.

For each product that a user can add in cart there is a table like this:

在此输入图像描述

In this table the user enters the quantity for the specified size and color. What would be the best way to store this values in a database table so that I can easily get and display them later?

I'm using PHP and a MySQL database.

Now my table looks like bellow but I'm having trouble getting the values when someone requests more than one size per color.

CREATE TABLE IF NOT EXISTS `site_platform_cart` (
  `cart_id` int(10) unsigned NOT NULL auto_increment,
  `cart_product_id` int(10) unsigned NOT NULL,
  `cart_product_name` varchar(256) collate latin1_general_ci NOT NULL,
  `cart_product_photo` varchar(256) collate latin1_general_ci NOT NULL,
  `cart_product_color_name` varchar(128) collate latin1_general_ci NOT NULL,
  `cart_product_color_image` varchar(128) collate latin1_general_ci NOT NULL,
  `cart_product_color` tinyint(4) NOT NULL,
  `cart_product_size` tinyint(2) NOT NULL,
  `cart_product_quantity` tinyint(3) unsigned NOT NULL,
  `cart_product_price` varchar(12) collate latin1_general_ci NOT NULL,
  `cart_date` date NOT NULL,
  `cart_user_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`cart_id`)
) ENGINE=MyISAM

Thank you.

This is what I would do:

Add a new row in the database for each Color/Size combination. So something like this:

INSERT INTO chart (user_id,item_id,color,size,quantity,cart_date) VALUES (.....

You could also, which might be a better idea, add the Chart items in the $_SESSION . This way your database isn't getting polluted

I would normalize this data in the database as pairs. Have a column for the user, the product, the color, the size, and the quantity ordered. Each square on the UI you posted would be a row in the database.

That would be flexible as you could easily add more products, colors, sizes, etc. Querying for a user, a product, colors, etc would all be very easy.

Your cart is nothing more than a representation of a sales order, and you should remember this. You need two tables (at minimum) to represent a sales order: one containing information on the overall order, the other containing information on all of the lines of the order.

Hopefully you have some kind of an 'Items' table, and hopefully each item key is different for each color/size combination (in other words, each color/size combo is a separate product).

If it is, simply store the item number and quantity in the line table for the order as you would in any other store.

There is no best way. I would use a table that stores all possible colors. A second table to store all possible sizes. A third order table would contain quantities and references to the size and color tables.

Okay, I'm going to put in my two cents' worth after deliberating over exactly what you are trying to achieve here.

The most pressing problem, I think, is the way you've designed the database. I had to deal with a similar problem before, and what was finally used as a solution was something like this:

table: Colours
    ColourID* | ColourDescription

table: Sizes
    SizeID* | SizeDescription

table: GenericItem
    GenericID* | Item Description | Price | Etc

table: Item
    ItemID* | ParentItem | SizeID | ColourID | AvailableStock | Etc

The Colours and Sizes tables store every size and colour that is available within your produt range. The GenericItem table stores each product you stock. For example, a Joe Bloggs V-Neck T-Shirt. Finally, the Item table stores each unique item - eg: Large Red Joe Bloggs V-Neck T-Shirt. You give it a unique item ID, and can easily reference the additional information from the GenericItem table.

Now, in terms of the cart, you can have tables like such:

table: Cart
    CartID* | CartUserID | CartDate | Etc

This table would hold the information for each specific cart. And now, expanding on Topener's answer, you would separately hold information for each cart line:

table: CartLine
    CartLineID* | CartID | ItemID | Quantity | Etc

This way, you an keep your data much more normalised and easily accessible, and allows you to store each cart in the database.

To retrieve this data, all you would need to do is store the CartID inside the $_session and have all your queries stem from that. For example, to reshow order lines: SELECT * FROM CartLine WHERE CartID = $_SESSION['CartID']

To display the grid now, all you need to do is create an array based around something like SELECT * FROM Item WHERE ParentItem = 'GenericID' . In this example, I assume that you have the generic item's ID already as a variable somewhere to display all the information about it on the page. Then, you can use the information there to map each textbox to an index of the array.

Hopefully, this will point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM