本文章来给大家推荐一个不错的购物车效果,这里主要求包括了几个东西,一个是购物车类用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 < 1 ) unset($this->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实现对外的操作,也就是添加操作
代码如下 |
复制代码 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <title>Shopping Cart</title> <script src="js/jquery-1.2.6.pack.js" type="text/javascript"></script> <script src="js/jquery.color.js" type="text/javascript"></script> <script src="js/thickbox.js" type="text/javascript"></script> <script src="js/cart.js" type="text/javascript"></script> <link href="css/style.css" rel="stylesheet" type="text/css" media="screen" /> <link href="css/thickbox.css" rel="stylesheet" type="text/css" media="screen" /> <script type="text/javascript"> $(function() { $("form.cart_form").submit(function() { var title = "Your Shopping Cart"; var orderCode = $("input[name=order_code]", this).val(); var quantity = $("input[name=quantity]", this).val(); var url = "cart_action.php?order_code=" + orderCode + "&amp;amp;amp;amp;amp;amp;quantity=" + quantity + "&amp;amp;amp;amp;amp;amp;TB_iframe=true&amp;amp;amp;amp;amp;amp;height=400&amp;amp;amp;amp;amp;amp;width=780"; tb_show(title, url, false); return false; }); }); </script> </head> <body> <div id="container"> <h1>购物车</h1> <a href="cart.php?KeepThis=true&amp;amp;amp;amp;amp;amp;TB_iframe=true&amp;amp;amp;amp;amp;amp;height=400&amp;amp;amp;amp;amp;amp;width=780" title="Your Shopping Cart" class="thickbox">打开购物车</a> <hr /> <a href="cart_action.php?order_code=KWL-JFE&amp;amp;amp;amp;amp;amp;quantity=3&amp;amp;amp;amp;amp;amp;TB_iframe=true&amp;amp;amp;amp;amp;amp;height=400&amp;amp;amp;amp;amp;amp;width=780" title="Your Shopping Cart" class="thickbox">添加三个 KWL-JFE 到购物车</a> <hr /> <form class="cart_form" action="cart_action.php" method="get"> <input type="hidden" name="order_code" value="KWL-JFE" /> <label>KWL-JFE: <input class="center" type="text" name="quantity" value="1" size="3" ?></label> <input type="submit" name="submit" value="添加到购物车" /> </form> </div> </body> </html> |
还有就是cart.php这是我们的购物车
代码如下 |
复制代码 |
<?php include('shopping_cart.class.php'); session_start(); $Cart = new Shopping_Cart('shopping_cart'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <head> <title>Shopping Cart</title> <script src="js/jquery-1.2.6.pack.js" type="text/javascript"></script> <script src="js/jquery.color.js" type="text/javascript"></script> <script src="js/cart.js" type="text/javascript"></script> <link href="css/cart.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div id="container"> <h1>Shopping Cart</h1> <?php if ( $Cart->hasItems() ) : ?> <form action="cart_action.php" method="get"> <table id="cart"> <tr> <th>数量</th> <th>商品名称</th> <th>商品编号</th> <th>单价</th> <th>总价</th> <th>删除</th> </tr> <?php $total_price = $i = 0; foreach ( $Cart->getItems() as $order_code=>$quantity ) : $total_price += $quantity*$Cart->getItemPrice($order_code); ?> <?php echo $i++%2==0 ? "<tr>" : "<tr class='odd'>"; ?> <td class="quantity center"><input type="text" name="quantity[<?php echo $order_code; ?>]" size="3" value="<?php echo $quantity; ?>" tabindex="<?php echo $i; ?>" /></td> <td class="item_name"><?php echo $Cart->getItemName($order_code); ?></td> <td class="order_code"><?php echo $order_code; ?></td> <td class="unit_price">$<?php echo $Cart->getItemPrice($order_code); ?></td> <td class="extended_price">$<?php echo ($Cart->getItemPrice($order_code)*$quantity); ?></td> <td class="remove center"><input type="checkbox" name="remove[]" value="<?php echo $order_code; ?>" /></td> </tr> <?php endforeach; ?> <tr><td colspan="2"></td><td colspan="3" id="total_price">您的消费总金额是:¥<?php echo $total_price; ?></td></tr> </table> <input type="submit" name="update" value="保存购物车" /> </form> <?php else: ?> <p class="center">您还没有购物.</p> <?php endif; ?> <p><a href="load.php">加载简单的购物车</a></p> </div> </body> </html>
|
教程链接:http://www.phprm.com/develop/47608.html
随意转载~但请保留教程地址★