php购物车的代码
时间:2007-02-17 来源:PHP爱好者
<?
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(!$session && !$scid) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$session = md5(uniqid(rand()));
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
SetCookie("scid", "$session", time() + 14400);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} /* last number is expiration time in seconds, 14400 sec = 4 hrs */
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
class Cart {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function check_item($table, $session, $product) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(!$result) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$numRows = mysql_num_rows($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if($numRows == 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} else {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row = mysql_fetch_object($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function add_item($table, $session, $product, $quantity) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$qty = $this->check_item($table, $session, $product);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if($qty == 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "INSERT INTO $table (session, product, quantity) VALUES ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "('$session', '$product', '$quantity') ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} else {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quantity += $qty;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function delete_item($table, $session, $product) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "DELETE FROM $table WHERE session='$session' AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function modify_quantity($table, $session, $product, $quantity) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function clear_cart($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "DELETE FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function cart_total($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(mysql_num_rows($result) > 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT price FROM inventory WHERE product='$row->product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$invResult = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row_price = mysql_fetch_object($invResult);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$total += ($row_price->price * $row->quantity);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $total;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function display_contents($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$count = 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM inventory WHERE product='$row->product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result_inv = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row_inventory = mysql_fetch_object($result_inv);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["product"][$count] = $row_inventory->product;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["price"][$count] = $row_inventory->price;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["quantity"][$count] = $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["total"][$count] = ($row_inventory->price * $row->quantity);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["description"][$count] = $row_inventory->description;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$count++;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$total = $this->cart_total($table, $session);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["final"] = $total;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $contents;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function num_items($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$num_rows = mysql_num_rows($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $num_rows;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function quant_items($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quant = 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quant += $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $quant;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
?>
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
/*
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
This part contains a description of how to create the tables on your mysql server.
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# MySQL dump 6.0
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Host: localhost Database: kmartShopper
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#--------------------------------------------------------
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Server version 3.22.25
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Table structure for table 'inventory'
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
CREATE TABLE inventory (
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
product tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
quantity tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
id int(4) DEFAULT '0' NOT NULL auto_increment,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
description tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
price float(10,2) DEFAULT '0.00' NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
category char(1) DEFAULT '' NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY id (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
PRIMARY KEY (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY price (price)
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Table structure for table 'shopping'
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
CREATE TABLE shopping (
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
session tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
product tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
quantity tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
card tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
id int(4) DEFAULT '0' NOT NULL auto_increment,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY id (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
PRIMARY KEY (id)
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
*/
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
Example
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
<?
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
include("shoppingcart.php");
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$cart = new Cart;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$mysql_link = mysql_connect("localhost", "wwwrun", "");
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$mysql_select_db("kmartShopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartShopper */
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
?>
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
/* call functions like $cart->add_item and such, see the code. */
php爱好者站 http://www.phpfans.net dreamweaver|flash|fireworks|photoshop.
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(!$session && !$scid) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$session = md5(uniqid(rand()));
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
SetCookie("scid", "$session", time() + 14400);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} /* last number is expiration time in seconds, 14400 sec = 4 hrs */
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
class Cart {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function check_item($table, $session, $product) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(!$result) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$numRows = mysql_num_rows($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if($numRows == 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} else {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row = mysql_fetch_object($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function add_item($table, $session, $product, $quantity) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$qty = $this->check_item($table, $session, $product);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if($qty == 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "INSERT INTO $table (session, product, quantity) VALUES ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "('$session', '$product', '$quantity') ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
} else {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quantity += $qty;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function delete_item($table, $session, $product) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "DELETE FROM $table WHERE session='$session' AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function modify_quantity($table, $session, $product, $quantity) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query .= "AND product='$product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function clear_cart($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "DELETE FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function cart_total($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
if(mysql_num_rows($result) > 0) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT price FROM inventory WHERE product='$row->product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$invResult = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row_price = mysql_fetch_object($invResult);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$total += ($row_price->price * $row->quantity);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $total;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function display_contents($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$count = 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM inventory WHERE product='$row->product' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result_inv = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$row_inventory = mysql_fetch_object($result_inv);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["product"][$count] = $row_inventory->product;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["price"][$count] = $row_inventory->price;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["quantity"][$count] = $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["total"][$count] = ($row_inventory->price * $row->quantity);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["description"][$count] = $row_inventory->description;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$count++;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$total = $this->cart_total($table, $session);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$contents["final"] = $total;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $contents;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function num_items($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$num_rows = mysql_num_rows($result);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $num_rows;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
function quant_items($table, $session) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quant = 0;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$query = "SELECT * FROM $table WHERE session='$session' ";
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$result = mysql_query($query);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
while($row = mysql_fetch_object($result)) {
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$quant += $row->quantity;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
return $quant;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
}
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
?>
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
/*
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
This part contains a description of how to create the tables on your mysql server.
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# MySQL dump 6.0
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Host: localhost Database: kmartShopper
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#--------------------------------------------------------
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Server version 3.22.25
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Table structure for table 'inventory'
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
CREATE TABLE inventory (
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
product tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
quantity tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
id int(4) DEFAULT '0' NOT NULL auto_increment,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
description tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
price float(10,2) DEFAULT '0.00' NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
category char(1) DEFAULT '' NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY id (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
PRIMARY KEY (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY price (price)
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
# Table structure for table 'shopping'
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
#
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
CREATE TABLE shopping (
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
session tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
product tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
quantity tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
card tinytext NOT NULL,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
id int(4) DEFAULT '0' NOT NULL auto_increment,
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
KEY id (id),
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
PRIMARY KEY (id)
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
);
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
*/
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
Example
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
<?
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
include("shoppingcart.php");
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$cart = new Cart;
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$mysql_link = mysql_connect("localhost", "wwwrun", "");
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
$mysql_select_db("kmartShopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartShopper */
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
?>
c hina it power . c omz3TwOi1xYq9LKpUkDIxzuEQvB
/* call functions like $cart->add_item and such, see the code. */
php爱好者站 http://www.phpfans.net dreamweaver|flash|fireworks|photoshop.
相关阅读 更多 +