2011-03-10 36 views
1

我正在建設網上商店,並遇到問題。 我將擁有一個直接價格的產品(例如HTC Touch 2智能手機:299.00美元),但同時我將擁有基於規格組合的價格的產品:針對網店的MySQL數據庫結構

在此圖片中,您可以看到數據庫圖表,我認爲這會爲多價格的產品是好的: database diagram

的主要問題: 由於這是一個網上商店,人們將商品放進購物車。我認爲插入購物車的物品應該來自同一張桌子(在我們的例子中,應該是[combinations]桌子,因爲存儲了價格)。

下面是這些表的一些數據,只是爲了更加明確:

[products]

productid | productName 
1   | Nike T-Shirt 
2   | HTC Touch 2 Smartphone 

[specifications]

specId | productId | specName 
1  | 1   | Size 
2  | 1   | Color 

[specvalues]

specValueId | specId | svValue 
1    | 1  | L 
2    | 1  | XL 
3    | 2  | white 
4    | 2  | blue 
5    | 2  | red 

[combinations](物品放入購物車)

combinationId | price | description 
1    | 10  | White L Nike T-Shirt 
2    | 15  | White XL Nike T-Shirt 
3    | 11  | Blue L Nike T-Shirt 
4    | 16  | Blue XL Nike T-Shirt 
5    | 18  | Red XL Nike T-Shirt 

[combinationParts]

nmid | combinationId | specValueId 
1  | 1    | 1 
2  | 1    | 3 
3  | 2    | 2 
4  | 2    | 3 
5  | 3    | 1 
1  | 3    | 4 
2  | 4    | 2 
3  | 4    | 4 
4  | 5    | 2 
5  | 5    | 5 

我希望我的圖表和數據庫的人口有一定道理:)。

所以問題是如何存儲單價產品(HTC Touch 2智能手機),以便它可以像多種價格產品一樣添加到購物車中。

回答

0

你可能想看看OpenCarts數據庫。它實際上做了一個很好的工作...

本質:

創建一個表: 產品 產品選項(你的「 spcifications') 產品選項值(你的‘specvalues’)

每個產品將在上市‘產品表’,並有價格 在‘產品選項’表中,你基本上是列出不同的‘規格’來一個產品...
產品選項值表格列出了實際選項以及對基準價格(+/-)的更改...

要存儲完成的訂單,OpenCart具有基本相同的表格...訂單ID關聯)

接下來我撕掉了購物車功能來處理比賽的球員註冊...

基部「產品」是玩家的註冊 - $ 25 在「product_option」僅列出的值被存儲在所述「product_option_value」的「規範」 /「註冊類型」

對於「註冊類型」表... 他們可以註冊爲支付或支付&玩(支付&玩入迷你遊戲) 支付只是默認選項,沒有改變價格... 支付和播放增加$ 15的價格(總共$ 40)

我可以很容易地添加多個「Produc t_options「和」product_option_value「設置爲產品...每個或者從運行總計的產品中增加或減少...

至於腳本方面,它只需要幾個循環來查詢和構建帶有產品的數組陣列產品的選擇,因爲產品的子陣列和產品選項的值,每個產品的選擇陣列

的「product_option_value」表的子陣列

-- 
-- Table structure for table `product` 
-- 

CREATE TABLE IF NOT EXISTS `product` (
    `product_id` int(11) NOT NULL auto_increment, 
    `site_id` int(11) NOT NULL, 
    `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL, 
    `description` text character set utf8 collate utf8_unicode_ci NOT NULL, 
    `price` decimal(15,2) NOT NULL default '0.00', 
    `date_available` date NOT NULL, 
    `date_unavailable` date NOT NULL, 
    `status` int(1) NOT NULL default '0', 
    `date_added` datetime NOT NULL default '0000-00-00 00:00:00', 
    `date_modified` datetime NOT NULL default '0000-00-00 00:00:00', 
    PRIMARY KEY (`product_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2 ; 

-- 
-- Dumping data for table `product` 
-- 

INSERT INTO `product` (`product_id`, `site_id`, `name`, `description`, `price`, `date_available`, `date_unavailable`, `status`, `date_added`, `date_modified`) VALUES 
(1, 2, 'Player Registration', 'This year we have two options: Pay or Pay &amp; Play.<br />Pay &amp; Play allows you to enroll in the Flights Minigames for the weekend (Master''s Marks and Flights Doubles) and gives you twenty dollars worth of prize raffles. <br />Pay &amp; Play is a $60.00 value and is only avalible during pre-registration.', 25.00, '2011-03-01', '2011-03-31', 1, '2011-03-01 00:00:00', '2011-03-01 00:00:00'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `product_option` 
-- 

CREATE TABLE IF NOT EXISTS `product_option` (
    `product_option_id` int(11) NOT NULL auto_increment, 
    `product_id` int(11) NOT NULL, 
    `sort_order` int(3) NOT NULL default '0', 
    `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`product_option_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2 ; 

-- 
-- Dumping data for table `product_option` 
-- 

INSERT INTO `product_option` (`product_option_id`, `product_id`, `sort_order`, `name`) VALUES 
(1, 1, 1, 'Registration Type'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `product_option_value` 
-- 

CREATE TABLE IF NOT EXISTS `product_option_value` (
    `product_option_value_id` int(11) NOT NULL auto_increment, 
    `product_option_id` int(11) NOT NULL, 
    `product_id` int(11) NOT NULL, 
    `price` decimal(15,2) NOT NULL, 
    `prefix` char(1) collate utf8_bin NOT NULL, 
    `sort_order` int(3) NOT NULL, 
    `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`product_option_value_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `product_option_value` 
-- 

INSERT INTO `product_option_value` (`product_option_value_id`, `product_option_id`, `product_id`, `price`, `prefix`, `sort_order`, `name`) VALUES 
(1, 1, 1, 15.00, '+', 1, 'Pay &amp; Play'), 
(2, 1, 1, 0.00, '', 2, 'Pay'); 
2

你一針見血的頭部,當你說「我認爲插入購物車的物品應該來自同一張桌子」。

HTC手機應該存在於組合表中,僅僅作爲一個組合。使用從上面的例子中,插入另一個進入組合表,例如:

---------------+-------------+------------------------ 
combinationId |  price | description 
---------------+-------------+------------------------ 
6    |  299  | Base Model 
---------------+-------------+------------------------ 

這解決了imediate的問題,有沒有缺點。此外,當您的店鋪增長,並且HTC可能會發布具有不同價格區間的新型號時,您已經擁有了適合的數據庫結構。

例如

---------------+-------------+------------------------ 
combinationId |  price | description 
---------------+-------------+------------------------ 
6    |  299  | Base Model 
7    |  349  | Exclusive Edition 
---------------+-------------+------------------------ 
0

在我看來simples的解決方案將是給智能手機只有一個規格記錄只用一個specvalues記錄。這樣,您可以保持每個產品記錄的完整結構。 只有一個選項時,不要在productview中顯示任何可選選項。