• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php购物车实现代码_php实例

php 搞代码 3年前 (2022-01-25) 15次浏览 已收录 0个评论

ShopCar.php

 <BR><?php <BR>class Shopcar <BR>{ <BR>//商品列表 <BR>public $productList=array(); <BR>/** <BR>* <BR>* @param unknown_type $product 传进来的商品 <BR>* @return true 购物车里面没有该商品 <BR>*/ <BR>public function checkProduct($product) <BR>{ <BR>for($i=0;$iproductList);$i++ ) <BR>{ <BR>if($this->productList[$i]['name']==$product['name']) <BR>return $i; <BR>} <BR>return -1; <BR>} <BR>//添加到购物车 <BR>public function add($product) <BR>{ <BR>$i=$this->checkProduct($product); <BR>if($i==-1) <BR>array_push($this->productList,$product); <BR>else <BR>$this->productList[$i]['num']+=$product['num']; <BR>} <BR>//删除 <BR>public function delete($product) <BR>{ <BR>$i=$this->checkProduct($product); <BR>if($i!=-1) <BR>array_splice($this->productList,$i,1); <BR>} <BR>//返回所有的商品的信息 <BR>public function show() <BR>{ <BR>return $this->productList; <BR>} <BR>} <BR>


productList.html

 <BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <BR><title>Insert title here</title> <BR><script type="text/javascript" src='jquery.min.js'></script> <BR><script type="text/javascript"> <BR>function buy(i) <BR>{ <BR>var num=$(':input[name=num]')[i].value; <BR>var name=$('[name=name]')[i].innerHTML; <BR>var price=$('[name=price]')[i].innerHTML; <BR>alert(num+name+price); <BR>$.ajax({ <BR>type:'post', //传送的方式,get/post <BR>url:'index.php', //发送数据的地址 <BR>cache:'false', <BR>data:'num='+num+"&name="+name+"&price="+price, <BR>success:function(data) <BR>{ <BR>alert(data); <BR>} <BR>}) <BR>} <BR></script> <BR> <BR><body> <BR><table> <BR><tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr> <BR><tr><td>0</td><td><label>商品1</label></td><td><label>1</label> <BR></td><td></td><td><u><font color='blue'>购买</font></u></td></tr> <BR><tr><td>1</td><td><label>商品2</label></td><td><label>2</label> <BR></td><td></td><td>购买</td></tr> <BR><tr><td>2</td><td><label>商品3</label></td><td><label>1</label> <BR></td><td></td><td>购买</td></tr> <BR><tr><td>3</td><td><label>商品4</label></td><td><label>1</label> <BR></td><td></td><td>购买</td></tr> <BR><tr>查看购物车</tr> <BR></table> <BR> <BR> <BR>


index.php

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


show.php

 <BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <BR><title></title> <BR> <BR><body> <BR><table> <BR><tr><td>商品编号<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong></td><td>商品名称</td><td>价格</td><td>数量</td></tr> <BR><?php <BR>require 'Shopcar.class.php'; <BR>session_start(); <BR>$shopcar=unserialize($_SESSION['shopcar']); <BR>print_r($shopcar); <BR>$productList=$shopcar->productList; <BR>foreach ($productList as $product){ <BR>?> <BR><tr><td>1</td><td><label><?php echo $product['name']?></label></td><td><label><?php echo $product['price']?></label> <BR></td><td>' /></td></tr> <BR><?php }?> <BR></table> <BR> <BR> <BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php购物车实现代码_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址