本文章来给大家推荐一个不错的购物车效果,这里主要求包括了几个东西,一个是购物车类用php写的, 还有一个ajax操作用到了jquery,还有一个jquery插件thickbox了,下面我们来看看。
购物车类:shop_cart.php
购物车的操作:cart_action.php
首页:index.html
ajax操作用到了jquery,还有一个jquery插件thickbox
不多说了你可以先看看效果示例
shop_cart.php当然是购物车的核心,但是这个类很简单,因为他又引进了cart_action.php用于对外操作。所以这个类显得相当精简。
购物车类shop_cart.php
代码如下 复制代码
cart_name = $name;
$this->items = $_session[$this->cart_name];
}
/**
* setitemquantity() - set the quantity of an item.
*
* @param string $order_code the order code of the item.
* @param int $quantity the quantity.
*/
function setitemquantity($order_code, $quantity) {
$this->items[$order_code] = $quantity;
}
/**
* getitemprice() - get the price of an item.
*
* @param string $order_code the order code of the item.
* @return int the price.
*/
function getitemprice($order_code) {
// this is where the code taht retrieves prices
// goes. we'll just say everything costs $9.99 for this tutorial.
return 9.99;
}
/**
* getitemname() - get the name of an item.
*
* @param string $order_code the order code of the item.
*/
function getitemname($order_code) {
// this is where the code that retrieves product names
// goes. we'll just return something generic for this tutorial.
return 'my product (' . $order_code . ')';
}
/**
* getitems() - get all items.
*
* @return array the items.
*/
function getitems() {
return $this->items;
}
/**
* hasitems() - checks to see if there are items in the cart.
*
* @return bool true if there are items.
*/
function hasitems() {
return (bool) $this->items;
}
/**
* getitemquantity() - get the quantity of an item in the cart.
*
* @param string $order_code the order code.
* @return int the quantity.
*/
function getitemquantity($order_code) {
return (int) $this->items[$order_code];
}
/**
* clean() - cleanup the cart contents. if any items have a
* quantity less than one, remove them.
*/
function clean() {
foreach ( $this->items as $order_code=>$quantity ) {
if ( $quantity items[$order_code]);
}
}
/**
* save() - saves the cart to a session variable.
*/
function save() {
$this->clean();
$_session[$this->cart_name] = $this->items;
}
}
?>
对于cart_action,他实现了shop_cart类与index的中间作用,用于更新,删除,增加商品的操作。
cart_action.php
代码如下 复制代码
getitemquantity($_get['order_code'])+$_get['quantity'];
$cart->setitemquantity($_get['order_code'], $quantity);
}else{
if ( !empty($_get['quantity']) ) {
foreach ( $_get['quantity'] as $order_code=>$quantity){
$cart->setitemquantity($order_code, $quantity);
}
}
if ( !empty($_get['remove']) ) {
foreach ( $_get['remove'] as $order_code ) {
$cart->setitemquantity($order_code, 0);
}
}
}
$cart->save();
header('location: cart.php');
?>
还有就是index.html实现对外的操作,也就是添加操作
代码如下 复制代码
shopping cart
return false;
});
});
购物车
打开购物车
添加三个 kwl-jfe 到购物车
kwl-jfe:
还有就是cart.php这是我们的购物车
代码如下 复制代码
shopping cart
shopping cart
hasitems() ) : ?>
数量
商品名称
商品编号
单价
总价
删除
getitems() as $order_code=>$quantity ) :
$total_price += $quantity*$cart->getitemprice($order_code);
?>
getitemname($order_code); ?>
$getitemprice($order_code); ?>
$getitemprice($order_code)*$quantity); ?>
您的消费总金额是:¥
您还没有购物.
加载简单的购物车
1 2
http://www.bkjia.com/phpjc/631557.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631557.htmltecharticle本文章来给大家推荐一个不错的购物车效果,这里主要求包括了几个东西,一个是购物车类用php写的, 还有一个ajax操作用到了jquery,还有一...