here's the e-commerce website project i am working on
http://www.wootenmedia.com/php_cart/products.php
can anyone help me with how to do this
thanks
<?php
session_start();
include("db.php");
// include function files for this application
require_once('bookmark_fns.php');
//create short variable names
$username = $HTTP_POST_VARS['username'];
$passwd = $HTTP_POST_VARS['passwd'];
if ($username && $passwd)
// they have just tried logging in
{
if (login($username, $passwd))
{
// if they are in the database register the user id
$HTTP_SESSION_VARS['valid_user'] = $username;
}
else
{
// unsuccessful login
do_html_header('Problem:');
echo 'You could not be logged in.
You must be logged in to view this page.';
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"], $_GET["catid"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"], $_GET["catid"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"], $_GET["catid"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
// Check if this item already exists in the users cart table
//--mike added-- the catid = $catid----------------------------------------------------------------
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId =' $itemId' and catid = '$catid'");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cart(cookieId, itemId, qty, catid) values('" . GetCartId() . "', $itemId, $qty, $catid)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty, $catid);
}
}
function UpdateItem($itemId, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($itemId);
}
else
{
/*mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId and catid = $catid");
*/
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
// Uses an SQL delete statement to remove an item from
// the users cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
//--mike added catid = $catid------------------------------------------------------------------
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$totalCost = 0;
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
?>
<?
//do_html_header('Checkout');
include("bookmark_fns.php");
check_valid_user();
?>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="#1B1987">
<font face="verdana" size="1" color="white">
<b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="#1B1987">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="15%" height="25" bgcolor="#1B1987">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="15%" height="25" bgcolor="#1B1987">
<font face="verdana" size="1" color="white">
<b>Remove?</b>
</font>
</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="15%" height="25" valign="top">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25" valign="top">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
<?php// echo $row["catid"]; ?>
</font>
</td>
<td width="15%" height="25" valign="top">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
<!-- <hr size="1" color="#1B1987" NOSHADE> -->
</td>
<td width="15%" height="25" valign="top">
<font face="verdana" size="1" color="black">
<a href="checkout.php?action=remove_item&id=<?php echo $row["itemId"]; ?>"><img src="remove.jpg" width="51" height="13" alt="" border="0"></a>
</font>
<!--<hr size="1" color="#1B1987" NOSHADE>-->
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr width="100%">
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<font face="verdana" size="1" color="black">
<!--<a href="products.php"><< Keep Shopping</a>-->
</font>
<font face="verdana" size="1" color="black">
<!--<a href="login.html">Checkout>></a>-->
<!--<a href="log.php">Checkout>></a>-->
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
</table>
</form>