Page 1 of 1

shopping cart

Posted: Mon Mar 15, 2004 10:36 am
by negar
hello
I have a code for a shopping cart but it doesnt seem to work...i was wondering if anyone can help.
I have two tables: one called Products with the attributes ProdID (primary key), Product, Prodname,Proadtype, Supplier, Price and another called cart with the attributes cartID (primary key), cookieID, ProdID, qty.
The products table as all the items that can be purchased in it and cart should contain all the orders made.

here is the code:

Code: Select all

<?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($ProdID, $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 ProdID = $ProdID");
$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, ProdID, qty) values('" . GetCartId() . "', $ProdID, $qty)");
		}
		else
		{
// This item already exists in the users cart,
// we will update it instead
			
UpdateItem($ProdID, $qty);
		}
	}
	
	function UpdateItem($ProdID, $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($ProdID);
		}
		else
		{
mysql_query("update cart set qty = $qty where cookieID = '" . GetCartId() . "' and ProdID = $ProdID");
		}
	}
	
	function RemoveItem($ProdID)
	{
// 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 ProdID = $ProdID");
	}
	
	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.ProdID = Products.ProdID where cart.cookieID = '" . GetCartId() . "' order by Products.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='+ProdID+'&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">
						&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["Price"]);
				?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["ProdID"]; ?>" 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["Prodname"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["iPrice"], 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["ProdID"]; ?>">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="order2.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
}

?>
#

Thanx

[Edit: Added php tags for eyecandy... --JAM]

Posted: Mon Mar 15, 2004 10:52 am
by Bill H
You're posting an awful lot of code
a) without tags, making it hard to read
b) and without telling us anything about what's wrong.

"It doesn't work." Okay, what doesn't it do?

shopping cart

Posted: Mon Mar 15, 2004 11:01 am
by negar
it doesnt do anythin thats the problem...when i click on the add item link on the previous page (order2.php) i end up with a blank screen..hope this helps

Re: shopping cart

Posted: Mon Mar 15, 2004 11:37 am
by TheBentinel.com
negar wrote:it doesnt do anythin thats the problem...when i click on the add item link on the previous page (order2.php) i end up with a blank screen..hope this helps
So when you're on order2.php, there's a link that says "add" and it is a link to the code you pasted above? But when that code executes, you get a blank screen back?

That's a pretty big whack of code to get nothing at all back from. How about stripping all the PHP code from it and seeing if you get the HTML displayed? Maybe you're just not getting to the page at all.

If you find that you are getting to the page, then put all the functions back in (not the code that's executing, just the code that's in functions) and try it again. If that displays, then start putting the executing code back in a chunk at a time, testing it after each add.

When you add something, test, and get a blank screen back, you'll know where the problem is. Then we can nail it down.

shopping cart

Posted: Mon Mar 15, 2004 1:49 pm
by negar
i tried what u said about deleted the php code and i do see the html.
then i added in the additem function and i can see the page but i cant see the item...did doesnt add an item
can u help?

Re: shopping cart

Posted: Mon Mar 15, 2004 2:31 pm
by TheBentinel.com
negar wrote:i tried what u said about deleted the php code and i do see the html.
then i added in the additem function and i can see the page but i cant see the item...did doesnt add an item
can u help?
The goal of deleting the PHP code is get down to a page that at least loads. (You mentioned that the page was totally blank before) So you got a page to load, just HTML. Now you've added back the additem function and the page still loads.

So now add back all the other functions. Does it still load, it's not blank?

Then add back some of the PHP code that was not inside a function. Just a few lines at a time. After each addition, retest and see if the screen is blank.

Let us know when it goes blank on you and what you added when that happened.

shopping cart

Posted: Tue Mar 16, 2004 6:11 am
by negar
hey
yeah i dont get a blank screen for add item but it doesnt add the item either. also as soon as i add any other bit of code it goes blank.
Thanx for ur help

Re: shopping cart

Posted: Tue Mar 16, 2004 9:08 am
by TheBentinel.com
negar wrote:hey
yeah i dont get a blank screen for add item but it doesnt add the item either. also as soon as i add any other bit of code it goes blank.
Thanx for ur help
What I need to know is, what did you to make the screen go blank. Even if any random bit of code seems to do it, pick one, see the blank screen, then post that code. The problem you need to solve is, "Why does the page with all the code not produce any output?" Taking it all out and putting it back one piece at a time until it destroys the page is how you do that.

So, put as much back in as you can without it going blank. When it goes blank, post that code and indicate what you added back last.

shopping cart

Posted: Tue Mar 16, 2004 12:56 pm
by negar
this code doesnt produce a blank screen
<?
include("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["ProdID"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["ProdID"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["ProdID"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}

function AddItem($ProdID, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead


// Check if this item already exists in the users cart table
$result = mysql_query("select cookieID, ProdID, qty from cart where cookieID = '" . GetCartID() . "' and ProdID = $ProdID");
$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, ProdID, qty) values('" . GetCartID() . "', $ProdID, $qty)");
}
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



// $totalCost = 0;
$result = mysql_query("select * from cart inner join items on cart.ProdID = Products.ProdID where cart.cookieID = '" . GetCartID() . "' order by Products.Prodname asc");
}
?>
<html>
<head>
<title> Your Shopping Cart </title>
<script language="JavaScript">
function UpdateQty(item)
{
ProdID = item.name;
newQty = item.options[item.selectedIndex].text;

document.location.href = 'cart.php?action=update_item&ProdID='+ProdID+'&qty='+newQty;
}


</script>
</head>
<body bgcolor="#CCCCFF">
<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["qty"] * $row["Price"]);

?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["ProdID"]; ?>" 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["Prodname"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
<?php echo number_format($row["Price"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">

<a href="cart.php?action=remove_item&ProdID=<?php echo $row["ProdID"];?> ">Remove</a>

</font>
</td>
</tr>
<?
}
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["Price"]);

$totalCost = $totalCost + ($row["qty"] * $row["Price"]);
?>
<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="order2.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>

however when i add this code between the additem function and the showCart function the screen goes blank
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($ProdID, $qty);
}


function UpdateItem($ProdID, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead



if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($ProdID);
}
else
{
mysql_query("update cart set qty = $qty where cookieID = '" . GetCartID() . "' and ProdID = $ProdID");
}
}

function RemoveItem($ProdID)
{
// Uses an SQL delete statement to remove an item from
// the users cart



mysql_query("delete from cart where cookieID = '" . GetCartID() . "' and ProdID = $ProdID");
}
}


thanx...does that help?

Re: shopping cart

Posted: Tue Mar 16, 2004 1:12 pm
by TheBentinel.com
Wow, I'm sorry but I can't seem to get my head around this thing. Without poking on the code myself, I'm just not going to be able to help much.

Sorry, I gave it my best shot.

Best advice I can give you is to narrow it down. Figure out exactly what line of PHP code is making the screen go blank and when you have that, then you'll be ahead of the game.

The code is pretty badly written from a maintenance perspective. It's not like it's a bad script (I'm no judge of that), but it's hard to look at it and tell what's happening. Anytime you've got this kind of stuff:

Code: Select all

<?php
  }
?>
you've got a maintenance problem. But if you get it working, then you may not care about maintainability.

Posted: Wed Mar 17, 2004 8:09 am
by CoderGoblin
You may want to enable error tracking at the top of the code...

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
?>