sql語(yǔ)句是select * from 你先打印出來(lái)查詢的數(shù)據(jù),再輸出想要的數(shù)據(jù)在頁(yè)面就行了
在黔西等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,黔西網(wǎng)站建設(shè)費(fèi)用合理。
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
數(shù)據(jù)庫(kù)是建立一個(gè)購(gòu)物車的表的。用戶ID作外鏈。用戶選定一種商品,存商品相關(guān)屬性入表。用戶查看購(gòu)物車時(shí)只要把有該用戶ID的購(gòu)物信息讀出來(lái)就行了。維護(hù)這張表就可以體現(xiàn)出用戶的購(gòu)物行為了。
購(gòu)物車
有兩種實(shí)現(xiàn)方式,一種是保存在數(shù)據(jù)庫(kù),另外一種是session
保存在數(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ù)存一下就行的
這個(gè)需要看你的購(gòu)物車結(jié)構(gòu)。
一般一個(gè)屬性的商品是在一條數(shù)據(jù)里面,多次加入只是更改數(shù)量
不同屬性的商品應(yīng)該分多條數(shù)據(jù)保存。最后購(gòu)物車統(tǒng)計(jì)的是總的數(shù)量
我來(lái)解答一下你的疑惑
買了兩個(gè)產(chǎn)品。那就是執(zhí)行了兩次
insert
into
temp_table
(uid,productid,pnum,poneprice,ptotalprice)
如果
productid相同則,
pnum
=
pnum+1;
ptotalprice
=
pnum*poneprice
假設(shè)前提是
當(dāng)前兩條記錄的產(chǎn)品不同,那么購(gòu)物車列表則是循環(huán)讀取temp_table列出現(xiàn)有符合條件之產(chǎn)品,數(shù)量,價(jià)格。
修改2個(gè)產(chǎn)品數(shù)量的時(shí)候,
提交后,同樣的文本框pnum為一個(gè)數(shù)組,productid為一個(gè)數(shù)組
獲取pnum,productid,并且用
split分析后,
分別update
update
temp_table
set
pnum='".$pnum[0]."',ptotalprice='..省略.'
where
uid=自己的uid
and
productid='".$productid[0]."'
注意,這里數(shù)組下標(biāo)要對(duì)應(yīng)好,你可以用個(gè)循環(huán)。
最后,當(dāng)訂單下好之后,要把臨時(shí)表的數(shù)據(jù)轉(zhuǎn)移到正式表中,并且清理掉當(dāng)前這個(gè)用戶臨時(shí)表的內(nèi)容即可。