ShopCar.php
<BR><?php <BR>class Shopcar <BR>{ <BR>//商品列表 <BR>public $productList=array(); <BR>/** <BR>* <BR>* @param unknown_type $product 传进来的商品 <BR>* @return true 购物车里面没有该商品 <BR>*/ <BR>public function checkProduct($product) <BR>{ <BR>for($i=0;$iproductList);$i++ ) <BR>{ <BR>if($this->productList[$i]['name']==$product['name']) <BR>return $i; <BR>} <BR>return -1; <BR>} <BR>//添加到购物车 <BR>public function add($product) <BR>{ <BR>$i=$this->checkProduct($product); <BR>if($i==-1) <BR>array_push($this->productList,$product); <BR>else <BR>$this->productList[$i]['num']+=$product['num']; <BR>} <BR>//删除 <BR>public function delete($product) <BR>{ <BR>$i=$this->checkProduct($product); <BR>if($i!=-1) <BR>array_splice($this->productList,$i,1); <BR>} <BR>//返回所有的商品的信息 <BR>public function show() <BR>{ <BR>return $this->productList; <BR>} <BR>} <BR>
productList.html
<BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <BR><title>Insert title here</title> <BR><script type="text/javascript" src='jquery.min.js'></script> <BR><script type="text/javascript"> <BR>function buy(i) <BR>{ <BR>var num=$(':input[name=num]')[i].value; <BR>var name=$('[name=name]')[i].innerHTML; <BR>var price=$('[name=price]')[i].innerHTML; <BR>alert(num+name+price); <BR>$.ajax({ <BR>type:'post', //传送的方式,get/post <BR>url:'index.php', //发送数据的地址 <BR>cache:'false', <BR>data:'num='+num+"&name="+name+"&price="+price, <BR>success:function(data) <BR>{ <BR>alert(data); <BR>} <BR>}) <BR>} <BR></script> <BR> <BR><body> <BR><table> <BR><tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr> <BR><tr><td>0</td><td><label>商品1</label></td><td><label>1</label> <BR></td><td></td><td><u><font color='blue'>购买</font></u></td></tr> <BR><tr><td>1</td><td><label>商品2</label></td><td><label>2</label> <BR></td><td></td><td>购买</td></tr> <BR><tr><td>2</td><td><label>商品3</label></td><td><label>1</label> <BR></td><td></td><td>购买</td></tr> <BR><tr><td>3</td><td><label>商品4</label></td><td><label>1</label> <BR></td><td></td><td>购买</td></tr> <BR><tr>查看购物车</tr> <BR></table> <BR> <BR> <BR>
index.php
<BR><?php <BR>require 'Shopcar.class.php'; <BR>session_start(); <BR>$name=$_POST['name']; <BR>$num=$_POST['num']; <BR>$price=$_POST['price']; <BR>$product=array('name'=>$name,'num'=>$num,'price'=>$price); <BR>print_r($product); <BR>if(isset($_SESSION['shopcar'])) <BR>$shopcar=unserialize($_SESSION['shopcar']); <BR>else <BR>$shopcar=new Shopcar(); <BR>$shopcar->add($product); <BR>$_SESSION['shopcar']=serialize($shopcar); <BR>
show.php
<BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <BR><title></title> <BR> <BR><body> <BR><table> <BR><tr><td>商品编号<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong></td><td>商品名称</td><td>价格</td><td>数量</td></tr> <BR><?php <BR>require 'Shopcar.class.php'; <BR>session_start(); <BR>$shopcar=unserialize($_SESSION['shopcar']); <BR>print_r($shopcar); <BR>$productList=$shopcar->productList; <BR>foreach ($productList as $product){ <BR>?> <BR><tr><td>1</td><td><label><?php echo $product['name']?></label></td><td><label><?php echo $product['price']?></label> <BR></td><td>' /></td></tr> <BR><?php }?> <BR></table> <BR> <BR> <BR>