以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下,实例代码如下:
-
<?php
-
-
require_once 'cart.class.php';
-
session_start();
-
if(!isset($_SESSION['cart'])) {
-
$_SESSION['cart'] = new Cart;
-
}
-
$cart =& $_SESSION['cart'];
-
-
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){
-
$p = $_POST['p'];
-
$items = $cart->add($p);
-
}
-
if( ($_GET['action']=='remove')&&($_GET['key']!="") ) {
-
$items = $cart->remove($_GET['key']);
-
}
-
-
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){
-
$key = $_POST['key'];
-
$value = $_POST['value'];
-
for($i=0;$i<count($key);$i ){
-
$items = $cart->modi($key[$i],$value[$i]);
-
}
-
}
-
-
$items = $cart->getCart();
-
-
echo "<table border=1>";
-
setlocale(LC_MONETARY, 'it_IT');
-
foreach($items as $item){
-
echo "<tr><form method="post" action="tmp.php">";
-
echo "<td>ID:".$item['ID']."<input type=hidden name=key[] value=".$item['ID'].">";
-
echo "<td>产品:".$item['name'];
-
echo "<td>单价:".$item['price'];
-
echo "<td><input type=text name=value[] value=".$item['count'].">";
-
$sum = $item['count']*$item['price'];
-
echo "<td>合计:".round($sum,2);
-
echo "<td><input type=button value='删除' onclick="location='?action=remove&key=".$item['ID']."'">";
-
}
-
echo "<input type=hidden name=action value=modi>";
-
echo "<tr><td colspan=7><input type=submit />";
-
echo "</td></form></tr></table>";
-
-
-
?>
-
<hr>
-
<form method="post" action="tmp.php">
-
ID:<input type="text" name="p[]" />
-
品名:<input type="text" name="p[]" />
-
单价:<input type="text" name="p[]" />
-
数量:<input type="text" name="p[]" />
-
<input type=hidden name=action value=add>
-
<input type="submit" />
-
</form>
-
-
-
-
<?
-
-
-
-
-
-
-
-
-
-
-
-
-
Class Cart{
-
-
var $cart;
-
var $totalCount;
-
var $totalPrices;
-
-
-
-
-
-
-
-
-
-
-
-
function Cart(){
-
$this->totalCount = 0;
-
$this->totalPrice = 0;
-
$this->cart = array();
-
}
-
-
-
-
-
-
-
-
-
-
-
-
function add($item){
-
if(!is_array($item)||is_null($item)) return $this->cart;
-
if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
-
echo "价格和数量必须是数字";
-
return $this->cart;
-
}
-
reset($item);
-
$key = current($item);
-
if($key=="") return $this->cart;
-
if($this->_isExists($key)){
-
$this->cart[$key]['count'] = end($item);
-
return $this->cart;
-
}
-
-
$this->cart[$key]['ID'] = $key;
-
$this->cart[$key]['name'] = next($item);
-
$this->cart[$key]['price'] = next($item);
-
$this->cart[$key]['count'] = next($item);
-
-
return $this->cart;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
function remove($key="",$count=""){
-
if($key=="") {
-
$this->cart = array();
-
return true;
-
}
-
if(!array_key_exists($key,$this->cart)) return false;
-
if($count==""){
-
unset($this->cart[$key]);
-
}else{
-
$this->cart[$key]['count'] -= $count;
-
if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
-
}
-
return $this->cart;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
function modi($key,$value){
-
if(!$this->_isExists($key)) return $this->cart();
-
if($value<=0){
-
unset($this->cart[$key]);
-
return $this->cart;
-
}
-
$this->cart[$key]['count'] = $value;
-
return $this->cart;
-
}
-
-
-
-
-
-
-
-
-
function getCart(){
-
return $this->cart;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
function _isExists($key)
-
{
-
if(isset($this->cart[$key])&&!emptyempty($this->cart[$key])&&array_key_exists($key,$this->cart))
-
return true;
-
return false;
-
}
-
-
-
-
-
-
-
-
-
-
-
function isEmpty(){
-
return !count($this->cart);
-
}
-
-
-
-
-
-
-
-
-
-
-
function _stat(){
-
if($this->isEmpty()) return false;
-
foreach($this->cart as $item){
-
$this->totalCount = @end($item);
-
$this->totalPrices = @prev($item);
-
}
-
return true;
-
}
-
-
-
-
-
-
-
-
-
-
-
function totalPrices(){
-
if($this->_stat())
-
return $this->totalPrices;
-
return 0;
-
}
-
-
-
-
-
-
-
-
-
-
-
function totalCount(){
-
if($this->_stat())
-
return $this->totalCount;
-
return 0;
-
}
-
-
-
}
-
?>
-
(责任编辑:最模板) |