php购物车实现代码
程序员文章站
2022-10-05 20:54:51
shopcar.php 复制代码 代码如下:
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>
复制代码 代码如下:
<?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>
上一篇: session 的生命周期是多长