I've been playing around with some code found on the Adobe Macromedia website, in relation to a shopping cart. I've managed to get all the things I wanted to get working. However I cannot get one particular function to work as I want it to work.
In the sample code, for updating the quantities of the products, the code provided by Adobe Macromedia, enables the user to select from a drop down box the quantity of products they want to select, and the the total price of the product will increase according to selected number.
However what I have been trying to do, is to allow the user to input the desired quantity into a text field. I've tried to adapt what was done in the drop down box, but cannot get it to work.
It somebody could help it would be much appreciated!
<?php
include("db2.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["item_id"], $_GET["item_qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["item_id"], $_GET["item_qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["item_id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($item_id, $item_qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
$dbhandle = @mysql_connect($hostname, $username, $password);
$selected = @mysql_select_db("eshop",$dbhandle);
// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cart where item_id = $item_id");
$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(item_id, item_qty) values($item_id, $item_qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($item_id, $item_qty);
}
}
function UpdateItem($item_id, $item_qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
$dbhandle = @mysql_connect($hostname, $username, $password);
$selected = @mysql_select_db("eshop",$dbhandle);
if($item_qty == 0)
{
// Remove the item from the users cart
RemoveItem($item_id);
}
else
{
mysql_query("update cart set item_qty = $item_qty where item_id = $item_id");
}
}
function RemoveItem($item_id)
{
// Uses an SQL delete statement to remove an item from
// the users cart
$dbhandle = @mysql_connect($hostname, $username, $password);
$selected = @mysql_select_db("eshop",$dbhandle);
mysql_query("delete from cart where item_id = $item_id");
}
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
$dbhandle = @mysql_connect($hostname, $username, $password);
$selected = @mysql_select_db("eshop",$dbhandle);
$totalCost = 0;
$result = @mysql_query("select * from cart inner join items on cart.item_id = items.item_id where cart.item_id = items.item_id order by items.item_name asc");
?>
<html>
<head>
<title> Your Shopping Cart </title>
<script language="JavaScript">
function UpdateQty(item)
{
item_id = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&item_id='+item_id+'&item_qty='+newQty;
}
</script>
</head>
<body bgcolor="#ffffff">
<h1>Your Shopping Cart</h1>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="20%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<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["item_qty"] * $row["item_price"]);
?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["item_id"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["item_qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
<input name="item_qty" type="text" id="item_qty" size="5" value="<?php echo $item_qty; ?>" class="box" onKeyUp="UpdateQty(this);">
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["item_name"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["item_price"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&item_id=<?php echo $row["item_id"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="list_items.php"><< Keep Shopping</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>
</body>
</html>
<?php
}
?>