最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php 购物车程序

时间:2014-06-09 16:39来源: 作者: 点击:
这是自己开发用到的一个简单的购物车功能的php代码,用了几个文件没用数据库就实现了购物车这样做如果用户关了浏览器,购物车里的商品就会全部丢失哦,有需要的朋友可以改进一下,利

这是自己开发用到的一个简单的购物车功能的php代码,用了几个文件没用数据库就实现了购物车这样做如果用户关了浏览器,购物车里的商品就会全部丢失哦,有需要的朋友可以改进一下,利用数据库+session +cookie来实现会很好一些。

  1. <?php 
  2. class Shopcar  
  3. //商品列表 
  4. public  $productList=array(); 
  5. /** 
  6.  *  
  7.  * @param unknown_type $product 传进来的商品 
  8.  * @return true 购物车里面没有该商品  
  9.  */ 
  10. public function checkProduct($product
  11.  for($i=0;$i<count($this->productList);$i++ ) 
  12.  { 
  13.   if($this->productList[$i]['name']==$product['name'])    
  14.   return $i
  15.  } 
  16.  return -1; 
  17.  //添加到购物车 
  18.  public function add($product)  
  19.  $i=$this->checkProduct($product); 
  20.  if($i==-1) 
  21.  array_push($this->productList,$product); 
  22.  else  
  23.  $this->productList[$i]['num']+=$product['num'];   
  24. //删除 
  25. public function delete($product
  26.  $i=$this->checkProduct($product); 
  27.  if($i!=-1) 
  28.  array_splice($this->productList,$i,1); 
  29.  
  30. //返回所有的商品的信息 
  31. public function show() 
  32.   return $this->productList; 
  33. html 
  34. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
  35. <html> 
  36. <head> 
  37. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  38. <title>Insert title here</title> 
  39. <script type="text/javascript" src='jquery.min.js'></script> 
  40. <script type="text/javascript"
  41. function buy(i) 
  42. var num=$(':input[name=num]')[i].value; 
  43. var name=$('[name=name]')[i].innerHTML; 
  44. var price=$('[name=price]')[i].innerHTML; 
  45. alert(num+name+price); 
  46. $.ajax({ 
  47.   type:'post',                    //传送的方式,get/post 
  48.   url:'index.php',   //发送数据的地址 
  49.   cache:'false',  
  50.   data:'num='+num+"&name="+name+"&price="+price,              
  51.   success:function(data) 
  52.   { 
  53.   alert(data); 
  54.   } 
  55. }) 
  56.  
  57. </script> 
  58. </head> 
  59. <body> 
  60. <table> 
  61. <tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr> 
  62. <tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label> 
  63. </td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(0)'><u><font color='blue'>购买</font></u></a></td></tr> 
  64. <tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label> 
  65. </td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(1)'>购买</a></td></tr> 
  66. <tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label> 
  67. </td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(2)'>购买</a></td></tr> 
  68. <tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label> 
  69. </td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(3)'>购买</a></td></tr> 
  70. <tr><a href='show.php'>查看购物车</a></tr> 
  71. </table> 
  72. </body> 
  73. </html> 
  74. index.ph 
  75. <?php 
  76. require 'Shopcar.class.php'
  77. session_start(); 
  78. $name=$_POST['name']; 
  79. $num=$_POST['num']; 
  80. $price=$_POST['price']; 
  81. $product=array('name'=>$name,'num'=>$num,'price'=>$price); 
  82. print_r($product); 
  83. if(isset($_SESSION['shopcar'])) 
  84. $shopcar=unserialize($_SESSION['shopcar']); 
  85. else 
  86. $shopcar=new Shopcar(); 
  87. $shopcar->add($product); 
  88. $_SESSION['shopcar']=serialize($shopcar); 
  89. show.php 
  90. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
  91. <html> 
  92. <head> 
  93. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  94. <title></title> 
  95. </head> 
  96. <body> 
  97. <table> 
  98. <tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr> 
  99. <?php  
  100. require 'Shopcar.class.php'
  101. session_start(); 
  102. $shopcar=unserialize($_SESSION['shopcar']); 
  103. print_r($shopcar); 
  104. $productList=$shopcar->productList; 
  105. foreach ($productList as $product){ 
  106. ?> 
  107. <tr><td>1</td><td><label ><?php echo $product['name']?></label></td><td><label name='price'><?php echo $product['price']?></label> 
  108. </td><td><input  name='num' type='text' value='<?php echo $product['num']?>' /></td></tr> 
  109. <?php }?> 
  110. </table> 
  111. </body> 
  112. </html> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容