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

首頁 > 開發(fā) > PHP > 正文

php購物車實(shí)例

2024-05-04 21:48:56
字體:
供稿:網(wǎng)友

這里又提供一款php購物車實(shí)例代碼,這是一款適合種位要開發(fā)商城或購物系統(tǒng)參考的開發(fā)實(shí)例了,告訴你如果增加商品到購物并刪除,與購物車的數(shù)據(jù)庫設(shè)計(jì)實(shí)例.

inventory表,代碼如下:

  1. create table inventory (  
  2.   product tinytext not null,  
  3.   quantity tinytext not null,  
  4.   id int(4) default '0' not null auto_increment,  
  5.   description tinytext not null,  
  6.   price float(10,2) default '0.00' not null,  
  7.   category char(1) default '' not null,  
  8.   key id (id),  
  9.   primary key (id),  
  10.   key price (price)  
  11. ); 
  12. insert into inventory values ('硬盤','5','1','80g','5600','1'); 
  13. insert into inventory values ('cpu','12','2','p4-2.4g','6600','1'); 
  14. insert into inventory values ('dvd-rom','7','3','12x','2000','1'); 
  15. insert into inventory values ('主板','3','4','asus','5000','2'); 
  16. insert into inventory values ('顯示卡','6','5','64m','4500','1'); 
  17. insert into inventory values ('刻錄機(jī)','4','6','52w','3000','1'); 

shoping表

  1. create table shopping (  
  2.   session tinytext not null,  
  3.   product tinytext not null,  
  4.   quantity tinytext not null,  
  5.   card tinytext not null,  
  6.   id int(4) default '0' not null auto_increment,  
  7.   key id (id),  
  8.   primary key (id)  
  9. ); 

shoper表

  1. create database shopper; 
  2. use shopper; 
  3. create table shopping (  
  4.   session tinytext not null,  
  5.   product tinytext not null,  
  6.   quantity tinytext not null,  
  7.   card tinytext not null,  
  8.   id int(4) default '0' not null auto_increment,  
  9.   key id (id),  
  10.   primary key (id)  
  11. );  
  12. create table inventory (  
  13.   product tinytext not null,  
  14.   quantity tinytext not null,  
  15.   id int(4) default '0' not null auto_increment,  
  16.   description tinytext not null,  
  17.   price float(10,2) default '0.00' not null,  
  18.   category char(1) default '' not null,  
  19.   key id (id),  
  20.   primary key (id),  
  21.   key price (price)  
  22. ); 
  23. insert into inventory values ('硬盤','5','1','80g','5600','1'); 
  24. insert into inventory values ('cpu','12','2','p4-2.4g','6600','1'); 
  25. insert into inventory values ('dvd-rom','7','3','12x','2000','1'); 
  26. insert into inventory values ('主板','3','4','asus','5000','2'); 
  27. insert into inventory values ('顯示卡','6','5','64m','4500','1'); 
  28. insert into inventory values ('刻錄機(jī)','4','6','52w','3000','1');

main.php購物頁面,代碼如下:

  1. /*mysql --user=root --password=your_password */ 
  2.  
  3.  
  4. include("shoppingcart.php");  
  5. $cart = new cart; 
  6. $table="shopping"
  7.  
  8. /* 查詢并顯示所有存貨表中的信息 */ 
  9.     $query = "select * from inventory"
  10.     $invresult = mysql_query($query);  
  11.     if (!($invresult)) {  
  12.        echo "查詢失敗<br>"
  13.        exit
  14.     }  
  15.     echo "以下產(chǎn)品可供訂購∶"
  16.     echo "<table border=0>"
  17.     echo "<tr><td bgcolor=#aaccff>產(chǎn)品編號(hào)</td><td bgcolor=#aaccff>產(chǎn)品名稱</td><td bgcolor=#aaccff>單價(jià)</td>"
  18.     echo "<td bgcolor=#aaccff>剩余數(shù)量</td><td bgcolor=#aaccff>產(chǎn)品描述</td><td bgcolor=#aaccff>放入購物車</td></tr>"
  19.     while($row_inventory = mysql_fetch_object($invresult)) { 
  20.     echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";  
  21.     echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";  
  22.     echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";  
  23.     echo "<td bgcolor=#aaccff>".$row_inventory->quantity."</td>";  
  24.     echo "<td bgcolor=#aaccff>".$row_inventory->description."</td>"
  25.     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>";  
  26.     } 
  27.     echo "</table>"
  28.     echo "<br>購物車中產(chǎn)品的數(shù)量∶".$cart->quant_items($table$session); 
  29.     echo "<br><br><a href='clearcart.php'><img border='0' src='car.gif'></a>清空購物車"
  30.  
  31.  
  32.  //shoppingcart.php代碼 
  33.  
  34.  <?php 
  35.     if (!$session && !$s) {  
  36.         $s = md5(uniqid(rand()));  
  37.         setcookie("session""$s", time() + 14400);  
  38.     } 
  39.  
  40. /* 檢查是否有 seesion, 如果沒有產(chǎn)生一個(gè) md5 的唯一 id, 并利用 cookie 存入 $s 中。 
  41. 并且設(shè)置其存在時(shí)間為 14400 sec 也就是 4 小時(shí) */ 
  42.  
  43.  
  44.     $mysql_link = mysql_connect("127.0.0.1""root""test");  
  45.     if (!($mysql_link)) {  
  46.        echo "連接數(shù)據(jù)庫失敗<br>"
  47.        exit
  48.     }  
  49.     $mysql_select=mysql_select_db("shopper"$mysql_link); 
  50.     if (!($mysql_select)) {  
  51.        echo "打開數(shù)據(jù)庫失敗<br>"
  52.        exit
  53.     }  
  54.  
  55. /* 購物車 class */ 
  56.  
  57.    class cart {  
  58.         function check_item($table$session$product) {  
  59.             $query = "select * from $table where session='$session' and product='$product' ";  
  60.             $result = mysql_query($query);  
  61.               
  62.             if(!$result) {  
  63.                 return 0;  
  64.             }  
  65.             $numrows = mysql_num_rows($result);  
  66.             if($numrows == 0) {  
  67.                 return 0;  
  68.             } else {  
  69.                 $row = mysql_fetch_object($result);  
  70.                 return $row->quantity;  
  71.             }  
  72.         } 
  73.  
  74.         function add_item($table$session$product$quantity) {  
  75.             $qty = $this->check_item($table$session$product);  
  76.             if($qty == 0) {  
  77.                 $query = "insert into $table (session, product, quantity) values ";  
  78.                 $query .= "('$session', '$product', '$quantity') ";  
  79.                 mysql_query($query);  
  80.             } else {  
  81.                 $quantity += $qty;  
  82.                 $query = "update $table set quantity='$quantity' where session='$session' and ";  
  83.                 $query .= "product='$product' ";  
  84.                 mysql_query($query); 
  85.             }  
  86.         }  
  87.           
  88.         function delete_item($table$session$product) {  
  89.             $query = "delete from $table where session='$session' and product='$product' ";  
  90.             mysql_query($query);  
  91.         }  
  92.           
  93.         function modify_quantity($table$session$product$quantity) {  
  94.             $query = "update $table set quantity='$quantity' where session='$session' ";  
  95.             $query .= "and product='$product' ";  
  96.             mysql_query($query);  
  97.         }  
  98.           
  99.         function clear_cart($table$session) {  
  100.             $query = "delete from $table where session='$session' ";  
  101.             mysql_query($query);  
  102.         }  
  103.           
  104.         function cart_total($table$session) {  
  105.             $query = "select * from $table where session='$session' ";  
  106.             $result = mysql_query($query);  
  107.             if(mysql_num_rows($result) > 0) {  
  108.                 while($row = mysql_fetch_object($result)) {  
  109.                     $query = "select price from inventory where product='$row->product' ";  
  110.                     $invresult = mysql_query($query);  
  111.                     $row_price = mysql_fetch_object($invresult);  
  112.                     $total += ($row_price->price * $row->quantity);  
  113.                 }  
  114.             }  
  115.             return $total;  
  116.         }  
  117.           
  118.  
  119.  
  120.                 $count++;  
  121.             }  
  122.     echo "</table>"
  123.             $total = $this->cart_total($table$session);  
  124.             $contents["final"] = $total;  
  125.             return $contents;  
  126.         }  
  127.           
  128.         function num_items($table$session) {  
  129.             $query = "select * from $table where session='$session' ";  
  130.             $result = mysql_query($query);  
  131.             $num_rows = mysql_num_rows($result);  
  132.             return $num_rows;  
  133.         }  
  134.           
  135.         function quant_items($table$session) {  
  136.             $quant = 0;  
  137.             $query = "select * from $table where session='$session' ";  
  138.             $result = mysql_query($query);  
  139.             while($row = mysql_fetch_object($result)) {  
  140.                 $quant += $row->quantity;  
  141.             }  
  142.             return $quant;  
  143.         }  
  144.     } 
  145. //增加到購物車 
  146. include("shoppingcart.php"); 
  147. $cart = new cart; 
  148. $table="shopping"
  149. echo "你的購物清單∶<br>"
  150. $cart->add_item($table,$session,$product,'1'); 
  151. $cart->display_contents($table$session); 
  152. echo "<br>你的購物總金額∶".$cart->cart_total($table$session); 
  153. echo "<br><form action='main.php'>"
  154. echo "<input type=submit value='繼續(xù)購物'>"
  155. echo "</form>";//開源代碼Vevb.com 
  156.  
  157.  
  158. //清空購物車 
  159.  
  160. include("shoppingcart.php"); 
  161. $cart = new cart; 
  162. $table="shopping"
  163. $cart->clear_cart($table$session); 
  164. echo "購物車中產(chǎn)品的數(shù)量∶".$cart->num_items($table$session); 
  165. echo "<form action='main.php'>"
  166. echo "<input type=submit value='繼續(xù)購物'>"
  167. echo "</form>";

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新闻| 福海县| 曲阜市| 满洲里市| 油尖旺区| 弥渡县| 灵寿县| 汉阴县| 雷州市| 新泰市| 柳江县| 广昌县| 隆德县| 和平县| 镇康县| 乡宁县| 铅山县| 庆城县| 靖宇县| 玉山县| 永嘉县| 瑞昌市| 遂宁市| 泸定县| 陇南市| 乡城县| 南汇区| 扎鲁特旗| 蓬溪县| 长岭县| 永丰县| 喀喇沁旗| 尉氏县| 疏勒县| 郎溪县| 崇阳县| 化州市| 突泉县| 济阳县| 江城| 稻城县|