要這樣,這樣你會有無數(shù)多的表,而且以后新的一個產(chǎn)品時候非常麻煩,如果要屬于新的類別,而且還會因?yàn)楸苊鈹?shù)據(jù)庫太復(fù)雜而使得許多不同類的產(chǎn)品歸在一個類。而且你的程序很麻煩,要為每個類編寫不同程序,因?yàn)閿?shù)據(jù)表名不同。
成都創(chuàng)新互聯(lián)公司主營西藏網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),西藏h5小程序開發(fā)搭建,西藏網(wǎng)站營銷推廣歡迎西藏等地區(qū)企業(yè)咨詢
應(yīng)該用下面的辦法,主要使用四個表存儲所有類別的商品:
第一、類別名稱表,字段有
類別ID,類別名稱
1 電腦
2 洗衣機(jī)
第二、類別屬性表,字段有:
類別ID,屬性ID,屬性名稱
1 1 CPU
1 2 內(nèi)存
1 3 屏幕尺寸
2 1 容量
2 2 類型
第三、商品名稱表,字段有:
商品ID,類別ID
1 1
2 1
3 2
4 2
第四、商品屬性表,字段有:
商品ID,屬性ID,屬性值
1 1 P4
1 2 128M
1 3 CRT 14
2 1 P4
2 2 512M
2 3 LCD19
3 1 9公斤
3 2 滾筒
4 1 8公斤
4 2 波輪
上面定義了四個商品,商品ID為1~4,分別是128M、512M內(nèi)存的電腦,和9公斤滾筒、8公斤的波輪洗衣機(jī)。
這樣定義的數(shù)據(jù)庫結(jié)構(gòu),可以包含任何商品,一般不會改變,那么程序也就無需改變,定義新的產(chǎn)品、或者修改現(xiàn)有商品只需要在程序界面有操作員點(diǎn)點(diǎn)鼠標(biāo)。
在數(shù)據(jù)庫里面用表(ShopingCart)
表有幾個基本字段
CartId 購物車ID(唯一)
Id 商品的ID(唯一)
Qty 用戶購買商品的數(shù)量
CreateDate 購物車創(chuàng)建時間
此表的字段CartId是區(qū)分不同用戶的購物車的ID,要是登陸用戶,你可以在表中存放此登陸用戶的登陸名的ID,要是未登陸用戶使用購物車,
如果是非登陸用戶
1、商品表建立商品基本信息主表CREATETABLEproduc。
2、cidint10NOTNULLCOMMENT分類。
3、titlevarchar100NOTNULLCOMMENT商品名稱。
4、ddtimeint10NOTNULLCOMMENT添加時間,PRIMARYKEY。
用php開發(fā)一個網(wǎng)上商城系統(tǒng)其實(shí)和大多數(shù)用php開發(fā)其他的系統(tǒng),或者用其他的語言開發(fā)一個系統(tǒng)的流程是一樣的。第一,在開發(fā)網(wǎng)上商城系統(tǒng)之前,得做好網(wǎng)上商城系統(tǒng)的需求分析。做好網(wǎng)上商城系統(tǒng)的需求分析對一個系統(tǒng)來說可謂是至關(guān)重要,需求分析指的是你到底要做一個怎樣的網(wǎng)上商城系統(tǒng),這個網(wǎng)上商城系統(tǒng)有什么功能,你要用這個網(wǎng)上商城系統(tǒng)做什么。說白了需求分析就是決定你什么的意思。第二,就要進(jìn)入到網(wǎng)上商城系統(tǒng)項(xiàng)目分析的流程了。網(wǎng)上商城系統(tǒng)的需求分析解決了做什么的問題,網(wǎng)上商城系統(tǒng)的項(xiàng)目分析解決怎么做的問題。第三,進(jìn)行完網(wǎng)上商城系統(tǒng)的項(xiàng)目分析后,就進(jìn)入到了人員的分工流程上來了。我們確定了怎么做,就要確定那些人做的問題。網(wǎng)上商城系統(tǒng)人員分工就是解決每個人做什么的問題。第四,進(jìn)行完人員分工的工作后就要進(jìn)行網(wǎng)上商城系統(tǒng)代碼的編寫流程了。這個流程就是要求分得了工作任務(wù)的人員在規(guī)定的時間內(nèi)把自己的工作完成。第五,代碼編寫完成后,就要進(jìn)行系統(tǒng)測試流程了。我們的系統(tǒng)要進(jìn)過各種各樣的測試,系統(tǒng)流暢性測試,系統(tǒng)穩(wěn)定性測試,系統(tǒng)安全性測試,只有通過了系統(tǒng)的測試,網(wǎng)上商城系統(tǒng)才可以交付用戶使用。第六,做完了系統(tǒng)測試之后,就可以交付給用戶使用了。
PHP Code
div id="products-wrapper"
h1Products/h1
div class="products"
?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$results = $mysqli-query("SELECT * FROM cart ORDER BY id ASC");
if ($results) {
//fetch results set as object and output HTML
while($obj = $results-fetch_object())
{
echo 'div class="product"';
echo 'form method="post" action="cart_update.php"';
echo 'div class="product-thumb"img src="images/'.$obj-product_img_name.'"/div';
echo 'div class="product-content"h3'.$obj-product_name.'/h3';
echo 'div class="product-desc"'.$obj-product_desc.'/div';
echo 'div class="product-info"';
echo 'Price '.$currency.$obj-price.' | ';
echo 'Qty input type="text" name="product_qty" value="1" size="3" /';
echo 'button class="add_to_cart"Add To Cart/button';
echo '/div/div';
echo 'input type="hidden" name="product_code" value="'.$obj-product_code.'" /';
echo 'input type="hidden" name="type" value="add" /';
echo 'input type="hidden" name="return_url" value="'.$current_url.'" /';
echo '/form';
echo '/div';
}
}
?
/div
div class="shopping-cart"
h2Your Shopping Cart/h2
?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo 'ol';
foreach ($_SESSION["products"] as $cart_itm)
{
echo 'li class="cart-itm"';
echo 'span class="remove-itm"a href="cart_update.php?removep='.$cart_itm["code"].'return_url='.$current_url.'"×/a/span';
echo 'h3'.$cart_itm["name"].'/h3';
echo 'div class="p-code"P code : '.$cart_itm["code"].'/div';
echo 'div class="p-qty"Qty : '.$cart_itm["qty"].'/div';
echo 'div class="p-price"Price :'.$currency.$cart_itm["price"].'/div';
echo '/li';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '/ol';
echo 'span class="check-out-txt"strongTotal : '.$currency.$total.'/strong a href="view_cart.php"Check-out!/a/span';
echo 'span class="empty-cart"a href="cart_update.php?emptycart=1return_url='.$current_url.'"Empty Cart/a/span';
}else{
echo 'Your Cart is empty';
}
?
/div
cart_update.php
PHP Code
?php
session_start();
include_once("config.php");
//empty cart by distroying current session
if(isset($_GET["emptycart"]) $_GET["emptycart"]==1)
{
$return_url = base64_decode($_GET["return_url"]); //return url
session_destroy();
header('Location:'.$return_url);
}
//add item in shopping cart
if(isset($_POST["type"]) $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$product_qty = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
$return_url = base64_decode($_POST["return_url"]); //return url
//limit quantity for single product
if($product_qty 10){
die('div align="center"This demo does not allowed more than 10 quantity!br /a href=""Back To Products/a./div');
}
//MySqli query - get details of item from db using product code
$results = $mysqli-query("SELECT product_name,price FROM cart WHERE product_code='$product_code' LIMIT 1");
$obj = $results-fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable
$new_product = array(array('name'=$obj-product_name, 'code'=$product_code, 'qty'=$product_qty, 'price'=$obj-price));
if(isset($_SESSION["products"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_itm["code"] == $product_code){ //the item exist in array
$product[] = array('name'=$cart_itm["name"], 'code'=$cart_itm["code"], 'qty'=$product_qty, 'price'=$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrive old info and prepare array for session var
$product[] = array('name'=$cart_itm["name"], 'code'=$cart_itm["code"], 'qty'=$cart_itm["qty"], 'price'=$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["products"] = array_merge($product, $new_product);
}else{
//found user item in array list, and increased the quantity
$_SESSION["products"] = $product;
}
}else{
//create a new session var if does not exist
$_SESSION["products"] = $new_product;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
//remove item from shopping cart
if(isset($_GET["removep"]) isset($_GET["return_url"]) isset($_SESSION["products"]))
{
$product_code = $_GET["removep"]; //get the product code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
$product[] = array('name'=$cart_itm["name"], 'code'=$cart_itm["code"], 'qty'=$cart_itm["qty"], 'price'=$cart_itm["price"]);
}
//create a new product list for cart
$_SESSION["products"] = $product;
}
//redirect back to original page
header('Location:'.$return_url);
}
?
view_cart.php
PHP Code
div class="view-cart"
?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["products"]))
{
$total = 0;
echo 'form method="post" action="paypal-express-checkout/process.php"';
echo 'ul';
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$results = $mysqli-query("SELECT product_name,product_desc, price FROM cart WHERE product_code='$product_code' LIMIT 1");
$obj = $results-fetch_object();
echo 'li class="cart-itm"';
echo 'span class="remove-itm"a href="cart_update.php?removep='.$cart_itm["code"].'return_url='.$current_url.'"×/a/span';
echo 'div class="p-price"'.$currency.$obj-price.'/div';
echo 'div class="product-info"';
echo 'h3'.$obj-product_name.' (Code :'.$product_code.')/h3 ';
echo 'div class="p-qty"Qty : '.$cart_itm["qty"].'/div';
echo 'div'.$obj-product_desc.'/div';
echo '/div';
echo '/li';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo 'input type="hidden" name="item_name['.$cart_items.']" value="'.$obj-product_name.'" /';
echo 'input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" /';
echo 'input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj-product_desc.'" /';
echo 'input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" /';
$cart_items ++;
}
echo '/ul';
echo 'span class="check-out-txt"';
echo 'strongTotal : '.$currency.$total.'/strong ';
echo '/span';
echo '/form';
}else{
echo 'Your Cart is empty';
}
?
/div
/div
本文名稱:php購物商城數(shù)據(jù)表設(shè)計(jì),基于php購物網(wǎng)站的設(shè)計(jì)與實(shí)現(xiàn)
分享網(wǎng)址:http://www.muchs.cn/article34/hcpppe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、云服務(wù)器、關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、移動網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)