Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Why doesn't my shopping cart script work?
Old 03-13-2004, 02:14 PM Why doesn't my shopping cart script work?
Funkyaardvark's Avatar
Novice Talker

Posts: 6
Location: Toronto, Canada
Trades: 0
Why doesn't this script work? An error comes up but it specifies that the last line is defective but the last line is </HTML>. What could be wrong?
Quote:
<?php

include("db.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
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

$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
$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) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($itemId, $qty);
}

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");
}
}

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);

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 = users.id where cart.cookieId = '" .

GetCartId() . "' order by users.prodname asc");

?>
<html><head><title> Your Shopping Cart </title><script language="JavaScript">

function UpdateQty(item) {

itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+n ewQty;}

</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">
&nbsp;&nbsp;<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["qty"] * $row["itemPrice"]);

?>
<tr>
<td width="15%" height="25">
<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">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?> </font> </td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 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["itemId"]; ?>">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="products.php">&lt;&lt; Keep Shopping</a>
</font> </td><td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo umber_format($totalCost, 2, ".", ","); ?></b>
</font></td></tr></table>
</form>
</body>
</html>
__________________
"Measuring a summer's day, I only find it slips away to grey. The hours they bring me pain." - Led Zeppelin
Funkyaardvark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-17-2004, 05:13 PM
sng
Junior Talker

Posts: 3
Location: CA
Trades: 0
Hello Funkyaardvark,

It's a bit hard to tell, but i think your AddItem function is missing a closing bracket.

Steven
sng is offline
Reply With Quote
View Public Profile Visit sng's homepage!
 
Reply     « Reply to Why doesn't my shopping cart script work?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23857 seconds with 12 queries