php购物车实现代码_PHP教程

WBOY
发布: 2016-07-21 15:24:09
原创
1548 人浏览过

ShopCar.php

复制代码 代码如下:

class Shopcar
{
//商品列表
public $productList=array();
/**
*
* @param unknown_type $product 传进来的商品
* @return true 购物车里面没有该商品
*/
public function checkProduct($product)
{
for($i=0;$iproductList);$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
复制代码 代码如下:





Insert title here










查看购物车
商品编号 商品名称 价格 数量 购买
0
购买
1
购买
2
购买
3
购买




index.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
复制代码 代码如下:










require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);
print_r($shopcar);
$productList=$shopcar->productList;
foreach ($productList as $product){
?>


商品编号 商品名称 价格 数量
1
' />




www.bkjia.comtruehttp://www.bkjia.com/PHPjc/324362.htmlTechArticleShopCar.php 复制代码 代码如下: ?php class Shopcar { //商品列表 public $productList=array(); /** * * @param unknown_type $product 传进来的商品 * @return true 购物车...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!