这是自己开发用到的一个简单的购物车功能的php代码,用了几个文件没用数据库就实现了购物车这样做如果用户关了浏览器,购物车里的商品就会全部丢失哦,有需要的朋友可以改进一下,利用数据库+session +cookie来实现会很好一些。
- <?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;
- }
- }
- 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.ph
- <?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>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php 购物车程序
波比源码 » php 购物车程序
Thank you great post. Hello Administ . deneme bonusu