| px | top | add code | search | signup | login | help |
<?php
// Shopping cart created by Ethan Schroeder 5-11-99
// To include this shopping cart, use include("shoppingcart.php3"); in your code
// Please note the parameters that you will need to call each function with.
// Every function here needs a $table and $session parameter to be passed in.
// You should create a $table variable in the script that calls this class to
// designate the table you are working with. Email questions to ethan@montana.com. The documentation is at: http://www.montana.com/ethan/shop.php3
// If you significantly modify this script, please email me with the changes so I
// can make it better.
// establishes the session variable so we know who is roaming around the web site.
// this session will only be established the first time that the parent page is loaded.
if(!$session && !$ShoppingCart) //make sure this hasn't already been established
{
$session = md5(uniqid(rand())); //creates a random session value
// sets a cookie with the value of session. On my pages, I do a simple test to see if
// the cookie exists on the user's machine ( if($ShoppingCart) ) if it does exist,
// I don't send the session ID's around with every page they visit, I just use the values
// in the cookie.
//set the cookie to remain for 2 hours
SetCookie("ShoppingCart","$session",time()+7200);
}
class Cart
{
function add_item($table,$session,$product, $quantity)
{
// Checks to see if they already have that product in their list
$in_list = "SELECT * FROM $table WHERE session='$session' ";
$in_list .= "AND product='$product'";
$result = mysql_query("$in_list");
$num_rows = mysql_num_rows($result);
// they don't have that product in their cart? Put it in.
if($num_rows == 0)
{
$sql = "INSERT INTO $table (session,product,quantity) VALUES ";
$sql .= "('$session','$product','$quantity')";
mysql_query("$sql");
}
// They have the product in their cart already? Add the quantity they specified
// to the product they have in their cart
else
{
$row = mysql_fetch_array($result);
$quantity = $quantity + $row[quantity];
$sql = "UPDATE $table SET quantity='$quantity' WHERE ";
$sql .= "session='$session' AND product='$product'";
mysql_query("$sql");
}
}
// delete a specified item
function delete_item($table,$session,$product)
{
mysql_query("DELETE FROM $table WHERE session='$session' AND product='$product'");
}
// modifies a quantity of an item
function modify_quantity($table, $session, $product, $quantity)
{
$sql = "UPDATE $table SET quantity='$quantity' ";
$sql .= "WHERE session='$session' AND product='$product'";
mysql_query("$sql");
}
// clear all content in their cart
function clear_cart($table,$session)
{
mysql_query("DELETE FROM $table WHERE session='$session'");
}
//add up the shopping cart total
function cart_total($table,$session)
{
$result = mysql_query("SELECT * FROM $table WHERE session='$session'");
if(mysql_num_rows($result) >0)
{
while($row = mysql_fetch_array($result))
{
// look up the item in inventory
$price_from_inventory = "SELECT price FROM inventory WHERE ";
$price_from_inventory .= "product = '$row[product]'";
$result_inventory = mysql_query("$price_from_inventory");
$row_price = mysql_fetch_array($result_inventory);
//calculate the total
$total = $total + ($row_price[price]*$row[quantity]);
}
}
return $total;
}
function display_contents($table,$session)
{
$count = 0;
$result = mysql_query("SELECT * FROM $table WHERE session='$session'");
while($row = mysql_fetch_array($result))
{
$result_inv = mysql_query("SELECT * FROM inventory WHERE product='$row[product]'");
$row_inventory = mysql_fetch_array($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]);
$count ++;
}
$total = $this->cart_total($table,$session);
$contents["final"] = $total;
return $contents;
}
function num_items($table, $session)
{
$result = mysql_query("SELECT * FROM $table WHERE session='$session'");
$num_rows = mysql_num_rows($result);
return $num_rows;
}
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.