} $this->cart[$key]['count'] = $value; return $this->cart; } /** * 返回当前购物车内商品的数组 * * @access public * @return array 返回当前购物车内商品的数组; */ function getCart(){ return $this->cart; } // }}} // {{{ _isExists($key) /** * 判断当前购物车中是否存在商品ID号为$key的商品 * * @access private * @param string $key 商品ID * @return bool true or false; */ function _isExists($key) { if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart)) return true; return false; } // }}} // {{{ isEmpty() /** * 判断当前购物车是否为空,即没有任何商品 * * @access public * @return bool true or false; */ function isEmpty(){ return !count($this->cart); } // }}} // {{{ _stat() /** * 取得部分统计信息 * * @access private * @return bool true or false; */ function _stat(){ if($this->isEmpty()) return false; foreach($this->cart as $item){ $this->totalCount = @end($item); $this->totalPrices = @prev($item); } return true; } // }}} // {{{ totalPrices() /** * 取得当前购物车所有商品的总金额 * * @access public * @return float 返回金额; */ function totalPrices(){ if($this->_stat()) return $this->totalPrices; return 0; } // }}} // {{{ isEmpty() /** * 取得当前购物车所有商品的总数量和 * * @access public * @return int ; */ function totalCount(){ if($this->_stat()) return $this->totalCount; return 0; } }//End Class Cart
?>
调用开始<?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'].">"; |