php购物车源码
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
?>
<tr><td>1</td><td><label><?phpecho $product['name']?></label></td><td><labelname='price'><?phpecho $product['price']?></label>
</td><td><inputname='num'type='text'value='<?php echo $product['num']?>' /></td></tr>
var name=$('[name=name]')[i].innerHTML;
var price=$('[name=price]')[i].innerHTML;
alert(num+name+price);
$.ajax({
type:'post', //传送的方式,get/post
url:'index.php', //发送数据的地址
<tr><td>0</td><td><labelname='name'>商品1</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(0)'><u><fontcolor='blue'>购买</font></u></a></td></tr>
$price=$_POST['price'];
$product=array('name'=>$name,'num'=>$num,'price'=>$price);
print_r($product);
if(isset($_SESSION['shopcar']))
$shopcar=unserialize($_SESSION['shopcar']);
?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<tr><td>2</td><td><labelname='name'>商品3</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(2)'>购买</a></td></tr>
<tr><ahref='show.php'>查看购物车</a></tr>
</table>
</body>
</html>
index.php
?
<?php
require'Shopcar.class.php';
session_start();
$name=$_POST['name'];
$num=$_POST['num'];
else
$shopcar=newShopcar();
$shopcar->add($product);
$_SESSION['shopcar']=serialize($shopcar);
show.php
?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
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>
php购物车实现
ShopCar.php
?
<?php
classShopcar
{
//商品列表
public$productList=array();
/**
*
* @param unknown_type $product传进来的商品
* @return true购物车里面没有该商品
*/
publicfunctioncheckProduct($product)
{
for($i=0;$i<count($this->productList);$i++ )
{
if($this->productList[$i]['name']==$product['name'])
return$i;
}
return-1;
}
//添加到购物车
publicfunctionadd($product)
<?php
require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);
print_r($pcar);
$productList=$shopcar->productList;
foreach ($productList as $product){
<tr><td>3</td><td><labelname='name'>商品4</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(3)'>购买</a></td></tr>
<?php}?>
</table>
</body>
</html>
{
$i=$this->checkProduct($product);
if($i!=-1)
array_splice($this->productList,$i,1);
}
//返回所有的商品的信息
publicfunctionshow()
{
return$this->productList;
}
}
productList.html
<title>Insert title here</title>
<scripttype="text/javascript"src='jquery.min.js'></script>
<scripttype="text/javascript">
function buy(i)
{
var num=$(':input[name=num]')[i].value;
{
$i=$this->checkProduct($product);
if($i==-1)
array_push($this->productList,$product);
else
$this->productList[$i]['num']+=$product['num'];
}
//删除
publicfunctiondelete($product)
<tr><td>1</td><td><labelname='name'>商品2</label></td><td><labelname='price'>2</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(1)'>购买</a></td></tr>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr>
<tr><td>1</td><td><label><?phpecho $product['name']?></label></td><td><labelname='price'><?phpecho $product['price']?></label>
</td><td><inputname='num'type='text'value='<?php echo $product['num']?>' /></td></tr>
var name=$('[name=name]')[i].innerHTML;
var price=$('[name=price]')[i].innerHTML;
alert(num+name+price);
$.ajax({
type:'post', //传送的方式,get/post
url:'index.php', //发送数据的地址
<tr><td>0</td><td><labelname='name'>商品1</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(0)'><u><fontcolor='blue'>购买</font></u></a></td></tr>
$price=$_POST['price'];
$product=array('name'=>$name,'num'=>$num,'price'=>$price);
print_r($product);
if(isset($_SESSION['shopcar']))
$shopcar=unserialize($_SESSION['shopcar']);
?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<tr><td>2</td><td><labelname='name'>商品3</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(2)'>购买</a></td></tr>
<tr><ahref='show.php'>查看购物车</a></tr>
</table>
</body>
</html>
index.php
?
<?php
require'Shopcar.class.php';
session_start();
$name=$_POST['name'];
$num=$_POST['num'];
else
$shopcar=newShopcar();
$shopcar->add($product);
$_SESSION['shopcar']=serialize($shopcar);
show.php
?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
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>
php购物车实现
ShopCar.php
?
<?php
classShopcar
{
//商品列表
public$productList=array();
/**
*
* @param unknown_type $product传进来的商品
* @return true购物车里面没有该商品
*/
publicfunctioncheckProduct($product)
{
for($i=0;$i<count($this->productList);$i++ )
{
if($this->productList[$i]['name']==$product['name'])
return$i;
}
return-1;
}
//添加到购物车
publicfunctionadd($product)
<?php
require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);
print_r($pcar);
$productList=$shopcar->productList;
foreach ($productList as $product){
<tr><td>3</td><td><labelname='name'>商品4</label></td><td><labelname='price'>1</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(3)'>购买</a></td></tr>
<?php}?>
</table>
</body>
</html>
{
$i=$this->checkProduct($product);
if($i!=-1)
array_splice($this->productList,$i,1);
}
//返回所有的商品的信息
publicfunctionshow()
{
return$this->productList;
}
}
productList.html
<title>Insert title here</title>
<scripttype="text/javascript"src='jquery.min.js'></script>
<scripttype="text/javascript">
function buy(i)
{
var num=$(':input[name=num]')[i].value;
{
$i=$this->checkProduct($product);
if($i==-1)
array_push($this->productList,$product);
else
$this->productList[$i]['num']+=$product['num'];
}
//删除
publicfunctiondelete($product)
<tr><td>1</td><td><labelname='name'>商品2</label></td><td><labelname='price'>2</label>
</td><td><inputname='num'type='text'value='1'/></td><td><aonclick='buy(1)'>购买</a></td></tr>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr>