購(gòu)物車有兩種實(shí)現(xiàn)方式,一種是保存在數(shù)據(jù)庫(kù),另外一種是session
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:成都鑿毛機(jī)等成都網(wǎng)站設(shè)計(jì)、全網(wǎng)整合營(yíng)銷推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。
保存在數(shù)據(jù)庫(kù)的不會(huì)以為關(guān)閉瀏覽器而消失,session會(huì)因?yàn)殛P(guān)閉瀏覽器就沒(méi)有了。
原理是把每個(gè)商品的信息存到一個(gè)數(shù)組里面,然后以這個(gè)商品的id作為鍵值,然后吧數(shù)組存到session里面就行,
如果是存入數(shù)據(jù)庫(kù)的話,就用關(guān)聯(lián)數(shù)據(jù)存一下就行的
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
用戶登陸后系統(tǒng)應(yīng)該會(huì)分配一個(gè)session : id,這個(gè)id應(yīng)該是改用于的用戶表id。然后再做一個(gè)購(gòu)物車的表,里面可以只考慮存商品id、用戶id等關(guān)鍵信息;
當(dāng)用戶提交購(gòu)買(mǎi)的時(shí)候再做相關(guān)的操作,具體可以參考淘寶、天貓的購(gòu)物車已經(jīng)后續(xù)付款效果。
參照ecshop購(gòu)物車功能。在核心文件,即客戶第一次訪問(wèn)網(wǎng)站既生成一個(gè)字符串session,設(shè)定session的生存時(shí)間,防止因客戶不操作而session一直存在。 cart表中就不說(shuō)了。客戶沒(méi)有登錄的時(shí)候用原始生成的session,添加購(gòu)物車就插入數(shù)據(jù)好了。如果客戶登錄了,修改cart表中的uid 。
訂單表,客戶信息表,商品信息表,購(gòu)物車表存放客戶信息表,商品信息表的主鍵id,用來(lái)關(guān)聯(lián),并且建立一個(gè)數(shù)量的字段,用來(lái)計(jì)算總價(jià)。客戶錄入表單去提交頁(yè)面添加判斷然后跳轉(zhuǎn)購(gòu)物頁(yè),添加購(gòu)物車的話直接基于數(shù)量和兩個(gè)主鍵id去添加,下單的話基于時(shí)間戳生成訂單編號(hào)和客戶編號(hào)存放在訂單表中,記得主外鍵關(guān)聯(lián)
新聞名稱:購(gòu)物車數(shù)據(jù)庫(kù)設(shè)計(jì)php,購(gòu)物商城數(shù)據(jù)庫(kù)設(shè)計(jì)
當(dāng)前地址:http://vcdvsql.cn/article34/hspose.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、企業(yè)建站、虛擬主機(jī)、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容