這里又提供一款php購物車實(shí)例代碼,這是一款適合種位要開發(fā)商城或購物系統(tǒng)參考的開發(fā)實(shí)例了,告訴你如果增加商品到購物并刪除,與購物車的數(shù)據(jù)庫設(shè)計(jì)實(shí)例.
inventory表,代碼如下:
- create table inventory (
- product tinytext not null,
- quantity tinytext not null,
- id int(4) default '0' not null auto_increment,
- description tinytext not null,
- price float(10,2) default '0.00' not null,
- category char(1) default '' not null,
- key id (id),
- primary key (id),
- key price (price)
- );
- insert into inventory values ('硬盤','5','1','80g','5600','1');
- insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
- insert into inventory values ('dvd-rom','7','3','12x','2000','1');
- insert into inventory values ('主板','3','4','asus','5000','2');
- insert into inventory values ('顯示卡','6','5','64m','4500','1');
- insert into inventory values ('刻錄機(jī)','4','6','52w','3000','1');
shoping表
- create table shopping (
- session tinytext not null,
- product tinytext not null,
- quantity tinytext not null,
- card tinytext not null,
- id int(4) default '0' not null auto_increment,
- key id (id),
- primary key (id)
- );
shoper表
- create database shopper;
- use shopper;
- create table shopping (
- session tinytext not null,
- product tinytext not null,
- quantity tinytext not null,
- card tinytext not null,
- id int(4) default '0' not null auto_increment,
- key id (id),
- primary key (id)
- );
- create table inventory (
- product tinytext not null,
- quantity tinytext not null,
- id int(4) default '0' not null auto_increment,
- description tinytext not null,
- price float(10,2) default '0.00' not null,
- category char(1) default '' not null,
- key id (id),
- primary key (id),
- key price (price)
- );
- insert into inventory values ('硬盤','5','1','80g','5600','1');
- insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
- insert into inventory values ('dvd-rom','7','3','12x','2000','1');
- insert into inventory values ('主板','3','4','asus','5000','2');
- insert into inventory values ('顯示卡','6','5','64m','4500','1');
- insert into inventory values ('刻錄機(jī)','4','6','52w','3000','1');
main.php購物頁面,代碼如下:
- /*mysql --user=root --password=your_password */
- include("shoppingcart.php");
- $cart = new cart;
- $table="shopping";
- /* 查詢并顯示所有存貨表中的信息 */
- $query = "select * from inventory";
- $invresult = mysql_query($query);
- if (!($invresult)) {
- echo "查詢失敗<br>";
- exit;
- }
- echo "以下產(chǎn)品可供訂購∶";
- echo "<table border=0>";
- echo "<tr><td bgcolor=#aaccff>產(chǎn)品編號(hào)</td><td bgcolor=#aaccff>產(chǎn)品名稱</td><td bgcolor=#aaccff>單價(jià)</td>";
- echo "<td bgcolor=#aaccff>剩余數(shù)量</td><td bgcolor=#aaccff>產(chǎn)品描述</td><td bgcolor=#aaccff>放入購物車</td></tr>";
- while($row_inventory = mysql_fetch_object($invresult)) {
- echo "<tr><td bgcolor=#aaccff>".$row_inventory->id."</td>";
- echo "<td bgcolor=#aaccff>".$row_inventory->product."</td>";
- echo "<td bgcolor=#aaccff>".$row_inventory->price."</td>";
- echo "<td bgcolor=#aaccff>".$row_inventory->quantity."</td>";
- echo "<td bgcolor=#aaccff>".$row_inventory->description."</td>";
- 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>";
- }
- echo "</table>";
- echo "<br>購物車中產(chǎn)品的數(shù)量∶".$cart->quant_items($table, $session);
- echo "<br><br><a href='clearcart.php'><img border='0' src='car.gif'></a>清空購物車";
- //shoppingcart.php代碼
- <?php
- if (!$session && !$s) {
- $s = md5(uniqid(rand()));
- setcookie("session", "$s", time() + 14400);
- }
- /* 檢查是否有 seesion, 如果沒有產(chǎn)生一個(gè) md5 的唯一 id, 并利用 cookie 存入 $s 中。
- 并且設(shè)置其存在時(shí)間為 14400 sec 也就是 4 小時(shí) */
- $mysql_link = mysql_connect("127.0.0.1", "root", "test");
- if (!($mysql_link)) {
- echo "連接數(shù)據(jù)庫失敗<br>";
- exit;
- }
- $mysql_select=mysql_select_db("shopper", $mysql_link);
- if (!($mysql_select)) {
- echo "打開數(shù)據(jù)庫失敗<br>";
- exit;
- }
- /* 購物車 class */
- class cart {
- function check_item($table, $session, $product) {
- $query = "select * from $table where session='$session' and product='$product' ";
- $result = mysql_query($query);
- if(!$result) {
- return 0;
- }
- $numrows = mysql_num_rows($result);
- if($numrows == 0) {
- return 0;
- } else {
- $row = mysql_fetch_object($result);
- return $row->quantity;
- }
- }
- function add_item($table, $session, $product, $quantity) {
- $qty = $this->check_item($table, $session, $product);
- if($qty == 0) {
- $query = "insert into $table (session, product, quantity) values ";
- $query .= "('$session', '$product', '$quantity') ";
- mysql_query($query);
- } else {
- $quantity += $qty;
- $query = "update $table set quantity='$quantity' where session='$session' and ";
- $query .= "product='$product' ";
- mysql_query($query);
- }
- }
- function delete_item($table, $session, $product) {
- $query = "delete from $table where session='$session' and product='$product' ";
- mysql_query($query);
- }
- function modify_quantity($table, $session, $product, $quantity) {
- $query = "update $table set quantity='$quantity' where session='$session' ";
- $query .= "and product='$product' ";
- mysql_query($query);
- }
- function clear_cart($table, $session) {
- $query = "delete from $table where session='$session' ";
- mysql_query($query);
- }
- function cart_total($table, $session) {
- $query = "select * from $table where session='$session' ";
- $result = mysql_query($query);
- if(mysql_num_rows($result) > 0) {
- while($row = mysql_fetch_object($result)) {
- $query = "select price from inventory where product='$row->product' ";
- $invresult = mysql_query($query);
- $row_price = mysql_fetch_object($invresult);
- $total += ($row_price->price * $row->quantity);
- }
- }
- return $total;
- }
- $count++;
- }
- echo "</table>";
- $total = $this->cart_total($table, $session);
- $contents["final"] = $total;
- return $contents;
- }
- function num_items($table, $session) {
- $query = "select * from $table where session='$session' ";
- $result = mysql_query($query);
- $num_rows = mysql_num_rows($result);
- return $num_rows;
- }
- function quant_items($table, $session) {
- $quant = 0;
- $query = "select * from $table where session='$session' ";
- $result = mysql_query($query);
- while($row = mysql_fetch_object($result)) {
- $quant += $row->quantity;
- }
- return $quant;
- }
- }
- //增加到購物車
- include("shoppingcart.php");
- $cart = new cart;
- $table="shopping";
- echo "你的購物清單∶<br>";
- $cart->add_item($table,$session,$product,'1');
- $cart->display_contents($table, $session);
- echo "<br>你的購物總金額∶".$cart->cart_total($table, $session);
- echo "<br><form action='main.php'>";
- echo "<input type=submit value='繼續(xù)購物'>";
- echo "</form>";//開源代碼Vevb.com
- //清空購物車
- include("shoppingcart.php");
- $cart = new cart;
- $table="shopping";
- $cart->clear_cart($table, $session);
- echo "購物車中產(chǎn)品的數(shù)量∶".$cart->num_items($table, $session);
- echo "<form action='main.php'>";
- echo "<input type=submit value='繼續(xù)購物'>";
- echo "</form>";
新聞熱點(diǎn)
疑難解答