PHP教程.应用实例_PHP

WBOY
发布: 2016-06-01 12:40:53
原创
826 人浏览过

PHP/MySQL 购物车程序

if(!$session && !$scid) {
$session = md5(uniqid(rand()));
SetCookie("scid", "$session", time() 14400);
} /* 最后一个数字是以秒为单位的过期时间,14400 秒 = 4 小时 */

class Cart {
function check_item($table, $session, $product) {
$查询=“SELECT * FROM $table WHERE session='$session' AND Product='$product'”;
$结果 = mysql_query($query);

if(!$result) {
return 0;
}

$numRows = mysql_num_rows($result);

if($numRows == 0) {
return 0;
} else {
$row = mysql_fetch_object($result);
返回 $row->数量;
}
}

function add_item($table, $session, $product, $quantity) {
$qty = $this->check_item($table, $session, $product );
if($qty == 0) {
$query = "INSERT INTO $table (会话、产品、数量) VALUES ";
$query .= "('$session', '$product', '$quantity') ";
mysql_query($query);
} else {
$数量 = $数量;
$query = "更新$table SET数量='$quantity' WHERE session='$session' AND ";
$query .= "product='$product' ";
mysql_query($query);
}
}

function delete_item($table, $session, $product) {
$query = "从 $table 删除,其中 session='$session' AND 产品='$产品' ”;
mysql_query($query);
}

function modify_quantity($table, $session, $product, $quantity) {
$query = "更新 $table SET 数量='$quantity' WHERE session='$session' ”;
$query .= "AND Product='$product' ";
mysql_query($query);
}

function clear_cart($table, $session) {
$query = "从 $table WHERE session='$session' 中删除";
mysql_query($query);
}

function cart_total($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ";
$结果 = mysql_query($query);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)) {
$query = "从库存中选择价格,其中产品='$row->产品' ”;
$invResult = mysql_query($query);
$row_price = mysql_fetch_object($invResult);
$total = ($row_price->价格 * $row->数量);
}
}
返回 $total;
}

function display_contents($table, $session) {
$count = 0;
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
$结果 = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$query = "SELECT * FROM inventory WHERE Product='$row->product' ";
$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["数量"][$count] = $row->数量;
$contents["total"][$count] = ($row_inventory->价格 * $row->数量);
$contents["description"][$count] = $row_inventory->description;
$count ;
}
$total = $this->cart_total($table, $session);
$contents["final"] = $total;
返回$内容;
}

function num_items($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ";
$结果 = mysql_query($query);
$num_rows = mysql_num_rows($result);
返回 $num_rows;
}

function quant_items($table, $session) {
$quant = 0;
$query = "SELECT * FROM $table WHERE session='$session' ";
$结果 = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$quant = $row->quantity;
}
返回 $quant;
}
}
?>

/*
这部分包含如何在 mysql 服务器上创建表的描述。

# MySQL 转储 6.0
#
# 主机:localhost 数据库:kmartShopper
#------------------------ ----------------------------------
# 服务器版本 3.22.25

#
# 表“库存”的表结构
#
创建表 inventory (
产品 tinytext NOT NULL,
数量 tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
描述tinytext NOT NULL,
价格 float(10,2) DEFAULT '0.00' NOT NULL,
类别 char(1) DEFAULT ' NOT NULL,
KEY id (id) ,
PRIMARY KEY (id),
KEY 价格 (price)
);

#
# 表 'shopping' 的表结构
#
CREATE TABLE shopping (
sessiontinytext NOT NULL,
产品tinytext NOT NULL,
数量tinytext NOT NULL,
卡tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
KEY id (id),
PRIMARY KEY (id)
);
*/

示例

include("shoppingcart.php");
$cart = 新购物车;
$mysql_link = mysql_connect("localhost", "wwwrun", "");
$mysql_select_db("kmartShopper", $mysql_link) /* 嘿,使用您将 2 个表放在下面的任何数据库名称来代替 kmartShopper */
?>
/* 调用 $cart-> 等函数add_item之类的,看代码。 */

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!