加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php购物车实现代码

发布时间:2020-05-28 20:18:24 所属栏目:PHP 来源:互联网
导读:php购物车实现代码这个代码比较全,需要的朋友可以参考下。

ShopCar.php
<div class="codetitle"><a style="CURSOR: pointer" data="72988" class="copybut" id="copybut72988" onclick="doCopy('code72988')"> 代码如下:<div class="codebody" id="code72988">
<?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
<div class="codetitle"><a style="CURSOR: pointer" data="64702" class="copybut" id="copybut64702" onclick="doCopy('code64702')"> 代码如下:<div class="codebody" id="code64702">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Insert title here

<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);
}
})
}



<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr>
<tr><td>0</td><td></td><td>
</td><td></td><td>购买</td></tr>
<tr><td>1</td><td></td><td>
</td><td></td><td>购买</td></tr>
<tr><td>2</td><td></td><td>
</td><td></td><td>购买</td></tr>
<tr><td>3</td><td></td><td>
</td><td></td><td>购买</td></tr>
<tr>查看购物车</tr>
</table>



index.php
<div class="codetitle"><a style="CURSOR: pointer" data="42676" class="copybut" id="copybut42676" onclick="doCopy('code42676')"> 代码如下:<div class="codebody" id="code42676">
<?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
<div class="codetitle"><a style="CURSOR: pointer" data="82197" class="copybut" id="copybut82197" onclick="doCopy('code82197')"> 代码如下:<div class="codebody" id="code82197">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">



<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></td><td>
</td><td><input name='num' type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>



(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读