登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

php购物车实现代码 转载  

2014-03-22 09:25:08|  分类: PHP |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
ShopCar.php 
复制代码代码如下:

<?php 
class Shopcar 

//商品列表 
public $productList=array(); 
/** 

* @param unknown_type $product 传进来的商品 
* @return true 购物车里面没有该商品 
*/ 
public function checkProduct($product) 

for($i=0;$i<count($this->productList);$i++ ) 

if($this->productList[$i]['name']==$product['name']) 
return $i; 

return -1; 

//添加到购物车 
public function add($product) 

$i=$this->checkProduct($product); 
if($i==-1) 
array_push($this->productList,$product); 
else 
$this->productList[$i]['num']+=$product['num']; 

//删除 
public function delete($product) 

$i=$this->checkProduct($product); 
if($i!=-1) 
array_splice($this->productList,$i,1); 

//返回所有的商品的信息 
public function show() 

return $this->productList; 



productList.html 
复制代码代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
<script type="text/javascript" src='jquery.min.js'></script> 
<script type="text/javascript"> 
function buy(i) 

var num=$(':input[name=num]')[i].value; 
var name=$('[name=name]')[i].innerHTML; 
var price=$('[name=price]')[i].innerHTML; 
alert(num+name+price); 
$.ajax({ 
type:'post', //传送的方式,get/post 
url:'index.php', //发送数据的地址 
cache:'false', 
data:'num='+num+"&name="+name+"&price="+price, 
success:function(data) 

alert(data); 

}) 

</script> 
</head> 
<body> 
<table> 
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr> 
<tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label> 
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(0)'><u><font color='blue'>购买</font></u></a></td></tr> 
<tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label> 
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(1)'>购买</a></td></tr> 
<tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label> 
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(2)'>购买</a></td></tr> 
<tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label> 
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(3)'>购买</a></td></tr> 
<tr><a href='show.php'>查看购物车</a></tr> 
</table> 
</body> 
</html> 

index.php 
复制代码代码如下:

<?php 
require 'Shopcar.class.php'; 
session_start(); 
$name=$_POST['name']; 
$num=$_POST['num']; 
$price=$_POST['price']; 
$product=array('name'=>$name,'num'=>$num,'price'=>$price); 
print_r($product); 
if(isset($_SESSION['shopcar'])) 
$shopcar=unserialize($_SESSION['shopcar']); 
else 
$shopcar=new Shopcar(); 
$shopcar->add($product); 
$_SESSION['shopcar']=serialize($shopcar); 

show.php 
复制代码代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title></title> 
</head> 
<body> 
<table> 
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr> 
<?php 
require 'Shopcar.class.php'; 
session_start(); 
$shopcar=unserialize($_SESSION['shopcar']); 
print_r($shopcar); 
$productList=$shopcar->productList; 
foreach ($productList as $product){ 
?> 
<tr><td>1</td><td><label ><?php echo $product['name']?></label></td><td><label name='price'><?php echo $product['price']?></label> 
</td><td><input name='num' type='text' value='<?php echo $product['num']?>' /></td></tr> 
<?php }?> 
</table> 
</body> 
</html> 
  评论这张
 
阅读(598)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018