
July 14th, 2003, 08:26 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Shopping Cart Tutorial Problem
Hello everyone.
I'm having problems with the shopping cart tutorial. I'm trying to adapt it to my own MySQL database, so things may look slightly different. When I click on "add to cart" and go to cart.php, everything seems fine. However, when I click on the dropdown tag to change the qty, the "Keep Shopping" block at the bottom duplicates itself. What gives? I think this may be an HTML parsing problem, where the function is calling the HTML code twice, since the function is being called twice, but how do I solve it?
Code below:
#CART.PHP
PHP Code:
[SIZE=1]<?php
session_start();
setcookie("cart_id", session_id(), time() + ((3600 * 24) * 30));
?>
<html>
<head>
<title>Buy My Junk.com</title>
</head>
<body>
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
<!-- HEADER FOR LOGO, 468x60 BANNERS ----------------->
<td colspan="3">
<h1>Buy My Junk.com</h1>
</td>
</tr>
<tr>
<td colspan="2" align="right">
LINK | LINK | LINK | LINK | LINK
</td>
<td align="right">
<input type="text" size="10" value="Search"> <input type="button" value="Go!"></td>
</tr>
<tr>
<!-- LEFT COLUMN FOR LINKS --------------------------->
<td width="15%" valign="top">
Register
<!-- LOGIN TABLE --------------------------------->
<table border="1" width="100%">
<tr><td>Name: </td><td><input type="text" size="12" name="name"></td></tr>
<tr><td>Pass: </td><td><input type="password" size="12" name="pass"></td></tr>
<tr><td colspan="2"><input type="submit" name="login" value="Login"></td></tr>
</table>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
LINK<br>
</td>
<!-- CENTER COLUMN FOR TEXT, CONTENT ----------------->
<td width="70%" valign="top">
<h2>View Cart</h2>
<?php
// includes
require("./data/functions.php");
// connect to db
db_connect();
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
?>
<!-- RIGHT COLUMN FOR ADS ---------------------------->
<td width="15%">
</td>
</tr>
<tr>
<!-- FOOTER FOR COPYRIGHT INFO ------------------------>
<td colspan="3"> </td>
</tr>
</table>
</td></tr>
</table>
</body>
</html>[/SIZE]
AND HERE IS THE CODE FOR THE FUNCTION SHOWCART:
PHP Code:
function ShowCart()
{
$result = mysql_query("select * from cart inner join products on cart.item_id = products.item_id where cart.cookie_id = '" . GetCartId() . "' order by products.item_name asc")
or mysql_error($result);
?>
<script language="JavaScript">
function UpdateQty(item)
{
item_id = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href='cart.php?action=update_ite m&id='+item_id+'&qty='+newQty;
}
</script>
<?php
// once we've retrieved the list, each item is displayed as part of a table, as a table row
echo "<table border='1'>";
while($row = mysql_fetch_array($result))
{
// increment the total cost of all items
$totalCost += ($row["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["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select></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&id=<?php echo $row['item_id']; ?>">Remove</a>
</font></td></tr>
<?php
}
?>
<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='products.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>
<?php
}
I would appreciate any help you can give me.
Thanks.
--Brian
|