SHOPPING CART
Posted: Tue Oct 01, 2002 1:19 pm
I am working on an online catalog which integrates MySQL, PHP, and PayPal. This is working pretty well so far, but there are a couple of glitches that I can't solve.
This bit of code queries the product table in my database, displays title, price and a button to add the item to the PayPal shopping cart. This works great, except that the first product displayed (no matter how sort) won't add to the cart. The rest work like a charm.
I have two other small glitches that I cant solve, but I'll save them for another post.
This bit of code queries the product table in my database, displays title, price and a button to add the item to the PayPal shopping cart. This works great, except that the first product displayed (no matter how sort) won't add to the cart. The rest work like a charm.
Code: Select all
$sql="SELECT * FROM products WHERE cost > 0 AND description <> 'NULL' ORDER BY title ASC";
$result = @mysql_query($sql);
$num = @mysql_num_rows($result);
$i=0;
echo "<table><tr bgcolor="black"><td width="400"><b><font color="white">Title</font></b></td><td width="25"><b><font color="white">Price</font></b></td><td><b><font color="white">Add to Cart</font></b></td></tr></table>";
while($num > $i)
{
$id = @mysql_result($result,$i,"id");
$title = @mysql_result($result,$i,"title");
$cost = @mysql_result($result,$i,"cost");
$price = $cost * 1.2;
$price = number_format($price, 2, '.', '');
if($i % 2)
{
echo "<table><tr bgcolor="#757575"><td width="400" height="20"><a href="title.php?x=$id">$title</a></td><td width="25" height="20">$price</td><td height="20">
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="---paypal account---">
<input type="hidden" name="item_name" value="$title">
<input type="hidden" name="item_number" value="$id">
<input type="hidden" name="amount" value="$price">
<input type="image" src="theme\addtocart.gif" border="1" name="submit" alt="Add to Cart" valign="center">
<input type="hidden" name="add" value="1">
</form></td></tr></table>";
}
else
{
echo "<table><tr bgcolor="white"><td width="400" height="20"><a href="title.php?x=$id">$title</a></td><td width="25" height="20">$price</td><td height="20">
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="---paypal account---">
<input type="hidden" name="item_name" value="$title">
<input type="hidden" name="item_number" value="12345">
<input type="hidden" name="amount" value="$price">
<input type="image" src="theme\addtocart.gif" border="1" name="submit" alt="Add to Cart" valign="center">
<input type="hidden" name="add" value="1">
</form></td></tr></table>";
}
$i++;
}
@mysql_free_result($result);
echo "</table>";
}