微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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> 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐