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

首頁 > 開發 > PHP > 正文

php購物車實現代碼

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

關于購物車,這個是在電子商務方面使用的比較多,用戶選擇好自己的商品需要保存起來,最后去收銀臺,這很像我們實際生活的超市,所以我現來寫一個簡單的php購物車實例代碼,比較詳細只要一步步,處理好就OK了.

購物車會用到php文件:

main.php 顯示商品

additem.php把商品加入購物車

cearcart.php刪除購物車中的商品

shoppingcart.php 操作類

用戶的數據庫代碼如下:

  1. inventory 
  2.   create table inventory (  
  3.     product tinytext not null,  
  4.     quantity tinytext not null,  
  5.     id int(4) default '0' not null auto_increment,  
  6.     description tinytext not null,  
  7.     price float(10,2) default '0.00' not null,  
  8.     category char(1) default '' not null,  
  9.     key id (id),  
  10.     primary key (id),  
  11.     key price (price)  
  12.   ); 
  13.   insert into inventory values ('硬盤','5','1','80g','5600','1'); 
  14.   insert into inventory values ('cpu','12','2','p4-2.4g','6600','1'); 
  15.   insert into inventory values ('dvd-rom','7','3','12x','2000','1'); 
  16.   insert into inventory values ('主板www.111cn.net','3','4','asus','5000','2'); 
  17.   insert into inventory values ('顯示卡','6','5','64m','4500','1'); 
  18.   insert into inventory values ('刻錄機','4','6','52w','3000','1'); 
  19.  
  20.  shopping 
  21.   create table shopping (  
  22.     session tinytext not null,  
  23.     product tinytext not null,  
  24.     quantity tinytext not null,  
  25.     card tinytext not null,  
  26.     id int(4) default '0' not null auto_increment,  
  27.     key id (id),  
  28.     primary key (id)  
  29.   );  
  30.  shopper 
  31.  
  32.   create database shopper; 
  33.   use shopper; 
  34.   create table shopping (  
  35.     session tinytext not null,  
  36.     product tinytext not null,  
  37.     quantity tinytext not null,  
  38.     card tinytext not null,  
  39.     id int(4) default '0' not null auto_increment,  
  40.     key id (id),  
  41.     primary key (id)  
  42.   );  
  43.   create table inventory (  
  44.     product tinytext not null,  
  45.     quantity tinytext not null,  
  46.     id int(4) default '0' not null auto_increment,  
  47.     description tinytext not null,  
  48.     price float(10,2) default '0.00' not null,  
  49.     category char(1) default '' not null,  
  50.     key id (id),  
  51.     primary key (id),  
  52.     key price (price)  
  53.   ); 
  54.   insert into inventory values ('硬盤','5','1','80g','5600','1'); 
  55.   insert into inventory values ('cpu','12','2','p4-2.4g','6600','1'); 
  56.   insert into inventory values ('dvd-rom','7','3','12x','2000','1'); 
  57.   insert into inventory values ('主板111cn.net','3','4','asus','5000','2'); 
  58.   insert into inventory values ('顯示卡','6','5','64m','4500','1'); 
  59.   insert into inventory values ('刻錄機','4','6','52w','3000','1'); 

main.php 顯示購物車所有商品,代碼如下:

  1. include("shoppingcart.php");  
  2. $cart = new cart; 
  3. $table="shopping"
  4.  
  5. /* 查詢并顯示所有存貨表中的信息 */ 
  6.     $query = "select * from inventory"
  7.     $invresult = mysql教程_query($query);  
  8.     if (!($invresult)) {  
  9.        echo "查詢失敗<br>"
  10.        exit
  11.     }  
  12.     echo "以下產品可供訂購∶"
  13.     echo "<table border=0>"
  14.     echo "<tr><td bgcolor=#aaccff>產品編號</td><td bgcolor=#aaccff>產品名稱</td><td bgcolor=#aaccff>單價</td>"
  15.     echo "<td bgcolor=#aaccff>剩余數量</td><td bgcolor=#aaccff>產品描述</td><td bgcolor=#aaccff>放入購物車</td></tr>"
  16.     while($row_inventory = mysql_fetch_object($invresult)) { 
  17.     echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";  
  18.     echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";  
  19.     echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";  
  20.     echo "<td bgcolor=#aaccff>".$row_inventory->quantity."</td>";  
  21.     echo "<td bgcolor=#aaccff>".$row_inventory->description."</td>"
  22.     echo "<td bgcolor=#aaccff><a href='additem.php?product=".$row_inventory->product."'><img border='0' src='cart.gif' width='81' height='17'></a></td></tr>"//開源代碼Vevb.com 
  23.     } 
  24.     echo "</table>"
  25.     echo "<br>購物車中產品的數量∶".$cart->quant_items($table$session); 
  26.     echo "<br><br><a href='clearcart.php'><img border='0' src='car.gif'></a>清空購物車"

additem.php代碼,增加商品代碼:

  1. include("shoppingcart.php"); 
  2. $cart = new cart; 
  3. $table="shopping"
  4. echo "你的購物清單∶<br>"
  5. $cart->add_item($table,$session,$product,'1'); 
  6. $cart->display_contents($table$session); 
  7. echo "<br>你的購物總金額∶".$cart->cart_total($table$session); 
  8. echo "<br><form action='main.php'>"
  9. echo "<input type=submit value='繼續購物'>"
  10. echo "</form>"
  11.  
  12. //clearcart.php刪除商品,清除購物車代碼 
  13.  
  14. include("shoppingcart.php"); 
  15. $cart = new cart; 
  16. $table="shopping"
  17. $cart->clear_cart($table$session); 
  18. echo "購物車中產品的數量∶".$cart->num_items($table$session); 
  19. echo "<form action='main.php'>"
  20. echo "<input type=submit value='繼續購物'>"
  21. echo "</form>"

shoppingcart.php類,代碼如下:

  1. if (!$session && !$s) {  
  2.         $s = md5(uniqid(rand()));  
  3.         setcookie("session""$s", time() + 14400);  
  4.     } 
  5.  
  6. /* 檢查是否有 seesion, 如果沒有產生一個 md5 的唯一 id, 并利用 cookie 存入 $s 中。 
  7. 并且設置其存在時間為 14400 sec 也就是 4 小時 */ 
  8.  
  9.  
  10.     $mysql_link = mysql_connect("127.0.0.1""root""test");  
  11.     if (!($mysql_link)) {  
  12.        echo "連接數據庫失敗<br>"
  13.        exit
  14.     }  
  15.     $mysql_select=mysql_select_db("shopper"$mysql_link); 
  16.     if (!($mysql_select)) {  
  17.        echo "打開數據庫失敗<br>"
  18.        exit
  19.     }  
  20.  
  21. /* 購物車 class */ 
  22.      
  23.     class cart {  
  24.         function check_item($table$session$product) {  
  25.             $query = "select * from $table where session='$session' and product='$product' ";  
  26.             $result = mysql_query($query);  
  27.               
  28.             if(!$result) {  
  29.                 return 0;  
  30.             }  
  31.             $numrows = mysql_num_rows($result);  
  32.             if($numrows == 0) {  
  33.                 return 0;  
  34.             } else {  
  35.                 $row = mysql_fetch_object($result);  
  36.                 return $row->quantity;  
  37.             }  
  38.         } 
  39.  
  40.         function add_item($table$session$product$quantity) {  
  41.             $qty = $this->check_item($table$session$product);  
  42.             if($qty == 0) {  
  43.                 $query = "insert into $table (session, product, quantity) values ";  
  44.                 $query .= "('$session', '$product', '$quantity') ";  
  45.                 mysql_query($query);  
  46.             } else {  
  47.                 $quantity += $qty;  
  48.                 $query = "update $table set quantity='$quantity' where session='$session' and ";  
  49.                 $query .= "product='$product' ";  
  50.                 mysql_query($query); 
  51.             }  
  52.         }  
  53.           
  54.         function delete_item($table$session$product) {  
  55.             $query = "delete from $table where session='$session' and product='$product' ";  
  56.             mysql_query($query);  
  57.         }  
  58.           
  59.         function modify_quantity($table$session$product$quantity) {  
  60.             $query = "update $table set quantity='$quantity' where session='$session' ";  
  61.             $query .= "and product='$product' ";  
  62.             mysql_query($query);  
  63.         }  
  64.           
  65.         function clear_cart($table$session) {  
  66.             $query = "delete from $table where session='$session' ";  
  67.             mysql_query($query);  
  68.         }  
  69.           
  70.         function cart_total($table$session) {  
  71.             $query = "select * from $table where session='$session' ";  
  72.             $result = mysql_query($query);  
  73.             if(mysql_num_rows($result) > 0) {  
  74.                 while($row = mysql_fetch_object($result)) {  
  75.                     $query = "select price from inventory where product='$row->product' ";  
  76.                     $invresult = mysql_query($query);  
  77.                     $row_price = mysql_fetch_object($invresult);  
  78.                     $total += ($row_price->price * $row->quantity);  
  79.                 }  
  80.             }  
  81.             return $total;  
  82.         }  
  83.           
  84.         function display_contents($table$session) {  
  85.             $count = 0;  
  86.             $query = "select * from $table where session='$session' order by id ";  
  87.             $result = mysql_query($query);  
  88.             echo "<table border=0>"
  89.             echo "<tr><td bgcolor=#aaccff>產品編號</td><td bgcolor=#aaccff>產品名稱</td><td bgcolor=#aaccff>單價</td>"
  90.             echo "<td bgcolor=#aaccff>購買數量</td><td bgcolor=#aaccff>單項小計</td><td bgcolor=#aaccff>產品描述</td></tr>"
  91.             while($row = mysql_fetch_object($result)) {  
  92.                 $query = "select * from inventory where product='$row->product' ";  
  93.                 $result_inv = mysql_query($query);  
  94.                 $row_inventory = mysql_fetch_object($result_inv);  
  95.                 $contents["product"][$count] = $row_inventory->product;  
  96.                 $contents["price"][$count] = $row_inventory->price;  
  97.                 $contents["quantity"][$count] = $row->quantity;  
  98.                 $contents["total"][$count] = ($row_inventory->price * $row->quantity);  
  99.                 $contents["description"][$count] = $row_inventory->description; 
  100.                
  101.                 echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";  
  102.                 echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";  
  103.                 echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";  
  104.                 echo "<td bgcolor=#aaccff>".$row->quantity."</td>";  
  105.                 echo "<td bgcolor=#aaccff>".$contents["total"][$count]."</td>";  
  106.                 echo "<td bgcolor=#aaccff>".$row_inventory->description."</td></tr>"
  107.  
  108.                 $count++;  
  109.             }  
  110.     echo "</table>"
  111.             $total = $this->cart_total($table$session);  
  112.             $contents["final"] = $total;  
  113.             return $contents;  
  114.         }  
  115.           
  116.         function num_items($table$session) {  
  117.             $query = "select * from $table where session='$session' ";  
  118.             $result = mysql_query($query);  
  119.             $num_rows = mysql_num_rows($result);  
  120.             return $num_rows;  
  121.         }  
  122.           
  123.         function quant_items($table$session) {  
  124.             $quant = 0;  
  125.             $query = "select * from $table where session='$session' ";  
  126.             $result = mysql_query($query);  
  127.             while($row = mysql_fetch_object($result)) {  
  128.                 $quant += $row->quantity;  
  129.             }  
  130.             return $quant;  
  131.         }  
  132.     }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太原市| 依安县| 博乐市| 灵台县| 长武县| 通化县| 日喀则市| 中江县| 龙泉市| 普兰县| 舒兰市| 泽州县| 伊春市| 惠东县| 顺义区| 全州县| 来凤县| 徐汇区| 东城区| 寻甸| 武威市| 日土县| 辽阳县| 永福县| 扎赉特旗| 临沭县| 徐水县| 桑植县| 元谋县| 克拉玛依市| 陈巴尔虎旗| 湘潭县| 新巴尔虎左旗| 永德县| 营山县| 嘉善县| 浦县| 罗山县| 奇台县| 陕西省| 靖边县|