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

最模板

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

php 购物车程序

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

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

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