国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php 購物車程序

2024-05-04 21:56:54
字體:
來源:轉載
供稿:網友

這是自己開發用到的一個簡單的購物車功能的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> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永福县| 盖州市| 绩溪县| 南宁市| 类乌齐县| 铅山县| 油尖旺区| 绵竹市| 眉山市| 白山市| 凤山县| 长治县| 晋江市| 衡南县| 阿鲁科尔沁旗| 沅江市| 宁安市| 玉屏| 河源市| 大足县| 合阳县| 长泰县| 綦江县| 云阳县| 高阳县| 永城市| 宜川县| 常山县| 阳谷县| 辉县市| 连州市| 伊春市| 镇坪县| 淅川县| 休宁县| 永年县| 德州市| 祁阳县| 柳江县| 宜丰县| 青州市|