Could someone please help me, I need to store the total amount from a php shopping cart in a database. It is already storing the session id, the item and the quantity ordered in a sales order table. Here is the code I'm using (I'm quite new to this):
my_shoppingcart.php
<?
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.
// set the cookie to remain for 30 minutes
SetCookie( "ShoppingCart", "$session",time()+1800);
}
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;
}
}
?>
my_view_cart.php
<?
mysql_connect("localhost", "username", "password");
mysql_select_db( "test_db");
include( "my_shoppingcart.php");
$table = "sales_order";
$cart = new Cart;
if($add)
{
if($ShoppingCart)
$session = $ShoppingCart;
$result = mysql_query( "SELECT * FROM inventory WHERE product='$add'");
$row = mysql_fetch_array($result);
$cart->add_item($table,$session,$row[product],1);
}
if($remove)
{
if($ShoppingCart)
{
$session = $ShoppingCart;
}
$result = mysql_query( "SELECT * FROM inventory WHERE product='$remove'");
$row = mysql_fetch_array($result);
$cart->delete_item($table,$session,$row[product]);
}
if($modify)
{
$contents = $cart->display_contents($table,$session);
for($i = 0; $i < sizeof($quantity); $i++)
{
$oldquan = $contents[quantity][$i];
$product = $contents[product][$i];
$newquan = $quantity[$product];
$cart->modify_quantity($table,$session,$product,$newquan);
}
}
// End update cart
?>
<html>
<head>
<title>Shopping Cart</title>
<link rel="stylesheet" href="text_box.css" type="text/css">
<link rel="stylesheet" href="lotto.css" type="text/css">
</head>
<style>
<!--
.MyScrolls
{
scrollbar-base-color:#990000;
scrollbar-face-color:#990000;
scrollbar-shadow-color:#FFCC99;
scrollbar-highlight-color:#FFCC99;
scrollbar-3dlight-color:#990000;
scrollbar-darkshadow-color:#990000;
scrollbar-track-color:#FFCC99;
scrollbar-arrow-color:#FFCC99;
}
-->
</style>
<body bgcolor="#990000" link="#FFCC99" alink="#FFCC99" vlink="#FFCC99" text="#FFCC99" class="MyScrolls">
<?
$url = $PHP_SELF;
if(!$ShoppingCard)
$url .= "?session=$session";
echo "<FORM ACTION=$url method=post>\n";
?>
<a name="top"></a>
<table align=center border="0" cellpadding="5" cellspacing="0">
<font size="6" face="Copperplate Gothic Bold"><CENTER>Shopping
Cart Items</font></td>
</center>
</TABLE>
<p><center><TABLE BORDER=1 cellpadding=2></center>
<TR>
<TD><font size="4" face="Copperplate Gothic Bold">Product</font></TD>
<TD><font size="4" face="Copperplate Gothic Bold">Price</font></TD>
<TD><font size="4" face="Copperplate Gothic Bold">Quantity</font></TD>
<TD><font size="4" face="Copperplate Gothic Bold">Total</font></TD>
<TD> </TD>
<?
if($ShoppingCart)
{
$session = $ShoppingCart;
}
$contents = $cart->display_contents($table,$session);
if($contents[product][0] != "")
{
$x = 0;
while($x != $cart->num_items($table,$session))
{
echo "<TR><TD>".$contents[product][$x]. "</TD><TD>€".$contents[price][$x]. "</TD>\n";
$product = $contents[product][$x];
echo "<TD><INPUT TYPE=text size=3 name=quantity[$product] ";
echo "value=\"".$contents[quantity][$x]. "\"></TD>";
echo "<TD>€".$contents[total][$x]. "</TD>\n";
echo "<TD><A HREF=\"my_view_cart.php?remove=".urlencode($contents[product][$x]);
echo "".(!$ShoppingCart? "&session=$session": ""). "\">Remove</A>";
$x ++;
}
echo "</TD><TR></TABLE>";
echo "<BR><B><center>Total Order Amount: €".$cart->cart_total($table,$session). "</center></B>";
echo "<BR><center><INPUT TYPE=submit name=modify value=\"Recalculate Order\"></center>";
echo "<BR><BR>";
}
else
echo "<p><center>There are no items in your shopping cart.</center>";
?>
</FORM>
</BODY>
</HTML>
I'm really stuck, please help!!