Home >php教程 >php手册 >php购物车实现代码

php购物车实现代码

WBOY
WBOYOriginal
2016-05-25 16:38:464420browse

关于购物车,这个是在电子商务方面使用的比较多,用户选择好自己的商品需要保存起来,最后去收银台,这很像我们实际生活的超市,所以我现来写一个简单的php购物车实例代码,比较详细只要一步步,处理好就OK了.

购物车会用到php文件:

main.php 显示商品

additem.php把商品加入购物车

cearcart.php删除购物车中的商品

shoppingcart.php 操作类

用户的数据库代码如下:

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 ('主板www.111cn.net','3','4','asus','5000','2'); 
insert into inventory values ('显示卡','6','5','64m','4500','1'); 
insert into inventory values ('刻录机','4','6','52w','3000','1'); 
shopping 
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)  
);  
shopper 
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 ('主板111cn.net','3','4','asus','5000','2'); 
insert into inventory values ('显示卡','6','5','64m','4500','1'); 
insert into inventory values ('刻录机','4','6','52w','3000','1');

main.php 显示购物车所有商品,代码如下:

include("shoppingcart.php");  
$cart = new cart; 
$table="shopping"; 
/* 查询并显示所有存货表中的信息 */ 
$query = "select * from inventory"; 
$invresult = mysql教程_query($query);  
if (!($invresult)) {  
   echo "查询失败<br>"; 
   exit; 
}  
echo "以下产品可供订购∶"; 
echo "<table border=0>"; 
echo "<tr><td bgcolor=#aaccff>产品编号</td><td bgcolor=#aaccff>产品名称</td><td bgcolor=#aaccff>单价</td>"; 
echo "<td bgcolor=#aaccff>剩余数量</td><td bgcolor=#aaccff>产品描述</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=&#39;additem.php?product=".$row_inventory->product."&#39;><img  border=&#39;0&#39; src=&#39;cart.gif&#39;    style="max-width:90%"php购物车实现代码 " ></a></td></tr>"; //开源代码phprm.com 
} 
echo "</table>"; 
echo "<br>购物车中产品的数量∶".$cart->quant_items($table, $session); 
echo "<br><br><a href=&#39;clearcart.php&#39;><img  border=&#39;0&#39; src=&#39;car.gif&#39; alt="php购物车实现代码 " ></a>清空购物车"; 
additem.php代码,增加商品代码:
include("shoppingcart.php"); 
$cart = new cart; 
$table="shopping"; 
echo "你的购物清单∶<br>"; 
$cart->add_item($table,$session,$product,&#39;1&#39;); 
$cart->display_contents($table, $session); 
echo "<br>你的购物总金额∶".$cart->cart_total($table, $session); 
echo "<br><form action=&#39;main.php&#39;>"; 
echo "<input type=submit value=&#39;继续购物&#39;>"; 
echo "</form>"; 
//clearcart.php删除商品,清除购物车代码 
include("shoppingcart.php"); 
$cart = new cart; 
$table="shopping"; 
$cart->clear_cart($table, $session); 
echo "购物车中产品的数量∶".$cart->num_items($table, $session); 
echo "<form action=&#39;main.php&#39;>"; 
echo "<input type=submit value=&#39;继续购物&#39;>"; 
echo "</form>"; 
shoppingcart.php类,代码如下:
<?php
if (!$session && !$s) {  
    $s = md5(uniqid(rand()));  
    setcookie("session", "$s", time() + 14400);  
} 
/* 检查是否有 seesion, 如果没有产生一个 md5 的唯一 id, 并利用 cookie 存入 $s 中。 
并且设置其存在时间为 14400 sec 也就是 4 小时 */ 
$mysql_link = mysql_connect("127.0.0.1", "root", "test");  
if (!($mysql_link)) {
   echo "连接数据库失败<br>"; 
   exit;
}
$mysql_select=mysql_select_db("shopper", $mysql_link); 
if (!($mysql_select)) {  
   echo "打开数据库失败<br>"; 
   exit; 
}
/* 购物车 class */ 
class cart {
    function check_item($table, $session, $product) {  
        $query = "select * from $table where session=&#39;$session&#39; and product=&#39;$product&#39; ";  
        $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 .= "(&#39;$session&#39;, &#39;$product&#39;, &#39;$quantity&#39;) ";  
            mysql_query($query);  
        } else {  
            $quantity += $qty;  
            $query = "update $table set quantity=&#39;$quantity&#39; where session=&#39;$session&#39; and ";  
            $query .= "product=&#39;$product&#39; ";  
            mysql_query($query); 
        }  
    }  
      
    function delete_item($table, $session, $product) {  
        $query = "delete from $table where session=&#39;$session&#39; and product=&#39;$product&#39; ";  
        mysql_query($query);  
    }  
      
    function modify_quantity($table, $session, $product, $quantity) {  
        $query = "update $table set quantity=&#39;$quantity&#39; where session=&#39;$session&#39; ";  
        $query .= "and product=&#39;$product&#39; ";  
        mysql_query($query);  
    }  
      
    function clear_cart($table, $session) {  
        $query = "delete from $table where session=&#39;$session&#39; ";  
        mysql_query($query);  
    }  
      
    function cart_total($table, $session) {  
        $query = "select * from $table where session=&#39;$session&#39; ";  
        $result = mysql_query($query);  
        if(mysql_num_rows($result) > 0) {  
            while($row = mysql_fetch_object($result)) {  
                $query = "select price from inventory where product=&#39;$row->product&#39; ";  
                $invresult = mysql_query($query);  
                $row_price = mysql_fetch_object($invresult);  
                $total += ($row_price->price * $row->quantity);  
            }  
        }  
        return $total;  
    }  
      
    function display_contents($table, $session) {  
        $count = 0;  
        $query = "select * from $table where session=&#39;$session&#39; order by id ";  
        $result = mysql_query($query);  
        echo "<table border=0>"; 
        echo "<tr><td bgcolor=#aaccff>产品编号</td><td bgcolor=#aaccff>产品名称</td><td bgcolor=#aaccff>单价</td>"; 
        echo "<td bgcolor=#aaccff>购买数量</td><td bgcolor=#aaccff>单项小计</td><td bgcolor=#aaccff>产品描述</td></tr>"; 
        while($row = mysql_fetch_object($result)) {  
            $query = "select * from inventory where product=&#39;$row->product&#39; ";  
            $result_inv = mysql_query($query);  
            $row_inventory = mysql_fetch_object($result_inv);  
            $contents["product"][$count] = $row_inventory->product;  
            $contents["price"][$count] = $row_inventory->price;  
            $contents["quantity"][$count] = $row->quantity;  
            $contents["total"][$count] = ($row_inventory->price * $row->quantity);  
            $contents["description"][$count] = $row_inventory->description; 
           
            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->quantity."</td>";  
            echo "<td bgcolor=#aaccff>".$contents["total"][$count]."</td>";  
            echo "<td bgcolor=#aaccff>".$row_inventory->description."</td></tr>"; 
            $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=&#39;$session&#39; ";  
        $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=&#39;$session&#39; ";  
        $result = mysql_query($query);  
        while($row = mysql_fetch_object($result)) {  
            $quant += $row->quantity;  
        }  
        return $quant;  
    }
}


文章链接:

随便收藏,请保留本文地址!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php中缓冲输出实例代码 Next article:php 浏览器缓存