您的位置:

php购物车数据库原理,php购物车及订单思路

本文目录一览:

请教关于PHP购物车代码的数据库表和字段

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关于用数据库作为购物车的原理

我来解答一下你的疑惑

买了两个产品。那就是执行了两次

insert

into

temp_table

(uid,productid,pnum,poneprice,ptotalprice)

如果

productid相同则,

pnum

=

pnum+1;

ptotalprice

=

pnum*poneprice

假设前提是

当前两条记录的产品不同,那么购物车列表则是循环读取temp_table列出现有符合条件之产品,数量,价格。

修改2个产品数量的时候,

提交后,同样的文本框pnum为一个数组,productid为一个数组

获取pnum,productid,并且用

split分析后,

分别update

update

temp_table

set

pnum='".$pnum[0]."',ptotalprice='..省略.'

where

uid=自己的uid

and

productid='".$productid[0]."'

注意,这里数组下标要对应好,你可以用个循环。

最后,当订单下好之后,要把临时表的数据转移到正式表中,并且清理掉当前这个用户临时表的内容即可。

php session实现购物车的原理

session_start;

然后原理就是,建立一个数组,$_SESSION['wupin'][]=选中的物品的ID,然后就可以了,如果用户取消,就把他选择取消的那个ID的键从数组里删除就行。

网站购物车是怎么个原理。对数据库表配哪些字段想不明白。请假大侠们。

第一:做购物车,一般来说是不存入数据库这样数据量比较大并且查询效率慢,所以一般购物车都用Session,或Cookie来实现,建一个购物车实体类,大概有这些字段,商品ID,用户ID,数量...等这可以根据自己需要来设置,然后比如购买一件商品添加到购物车就创建一个hashtable来保存购物车里的信息,然后把hashtable保存到Session或Cookie,大致就这样。

第二:订单,你说的那个订单一般都有一个订单表的。首先要弄清楚流程,肯定是用户先将产品加入购物车,然后再提交订单的。为什么会订单下了以后还关购物车的事呢?购物车只是临时保存用户购买产品的地方。就像超市里去卖东西首先推一个车,然后去选购你要的产品,最后付款。对应这车只是你保存东西的一个工具,当你购买完毕后就不会和他有什么联系了。如果你后面需要退货你也只管那张单据(对应产品订单)而不会和你购物车车上联系。

不知道我这样讲你是否明白!

PHP中用json实现购物车功能,怎么实现

购物车中至少包含 以下信息:

商品ID,商品名称,商品价格,购买数量,(其他如果有)

把以上信息组成一个数组,

每购买一件商品,就生成一个这样的数组,多个数组组成一个大数组,

然后将这个二维数组转成JSON格式存入COOKIE即可。在购物车页面加载时,读取COOKIE内的这个JSON,然后再转成数组输出到页面即可。

PHP数组转JSON:json_encode

JSON转数组:json_decode

php 实现 购物车 提交到 订单的数据库

用户登陆后系统应该会分配一个session : id,这个id应该是改用于的用户表id。然后再做一个购物车的表,里面可以只考虑存商品id、用户id等关键信息;

当用户提交购买的时候再做相关的操作,具体可以参考淘宝、天猫的购物车已经后续付款效果。