Page 1 of 1

SHOPPING CART

Posted: Tue Oct 01, 2002 1:19 pm
by lloydsmods
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.

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)
	&#123;
		$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)
		&#123;
				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>";
		&#125;
		
		else
		&#123;
		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>";
		&#125;
	
	
		$i++;
	&#125;
    @mysql_free_result($result);
	echo "</table>";
&#125;
I have two other small glitches that I cant solve, but I'll save them for another post.

Posted: Wed Oct 02, 2002 2:15 am
by twigletmac
Try this:

Code: Select all

&lt;table&gt;
&lt;tr bgcolor="#000000"&gt;
	&lt;td width="400"&gt;
		&lt;b&gt;&lt;font color="#ffffff"&gt;Title&lt;/font&gt;&lt;/b&gt;
	&lt;/td&gt;
	&lt;td width="25"&gt;
		&lt;b&gt;&lt;font color="#ffffff"&gt;Price&lt;/font&gt;&lt;/b&gt;
	&lt;/td&gt;
	&lt;td&gt;
		&lt;b&gt;&lt;font color="#ffffff"&gt;Add to Cart&lt;/font&gt;&lt;/b&gt;
	&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt; 

&lt;?php
$sql= "SELECT id, title, cost FROM products WHERE cost &gt; 0 AND description &lt;&gt; 'NULL' ORDER BY title ASC"; 
$result = @mysql_query($sql) or die(mysql_error()); 

$bgcolor = '757575';
while($row = mysql_fetch_assoc($result)) { 
	$id = $row&#1111;'id'];    
	$title = $row&#1111;'title']; 
	$cost = $row&#1111;'cost']; 
	$price = number_format(($cost * 1.2), 2, '.', ''); 

	$bgcolor = ($bgcolor != '757575') ? '757575' : 'ffffff';

?&gt;
&lt;table&gt;
&lt;tr bgcolor="#&lt;?php echo $bgcolor; ?&gt;"&gt;
	&lt;td width="400" height="20"&gt;
		&lt;a href="title.php?x=&lt;?php echo $id; ?&gt;"&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;
	&lt;/td&gt;
	&lt;td width="25" height="20"&gt;&lt;?php echo $price; ?&gt;&lt;/td&gt;
	&lt;td height="20"&gt; 
		&lt;form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"&gt; 
			&lt;input type="hidden" name="cmd" value="_cart"&gt; 
			&lt;input type="hidden" name="business" value="---paypal account---"&gt; 
			&lt;input type="hidden" name="item_name" value="&lt;?php echo $title; ?&gt;"&gt; 
			&lt;input type="hidden" name="item_number" value="&lt;?php echo $id; ?&gt;"&gt; 
			&lt;input type="hidden" name="amount" value="&lt;?php echo $price; ?&gt;"&gt; 
			&lt;input type="image" src="theme\addtocart.gif" border="1" name="submit" alt="Add to Cart" valign="center"&gt; 
			&lt;input type="hidden" name="add" value="1"&gt; 
		&lt;/form&gt;
	&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;?php      
} // end while
?&gt;
Mac

WELL, NOT QUITE...

Posted: Sat Oct 05, 2002 12:08 am
by lloydsmods
That code is much more efficient than mine, no question, but the first product printed from the query still will not add to the shopping cart.

Posted: Sat Oct 05, 2002 6:41 am
by twigletmac
By 'will not add to the shopping cart' do you mean it won't show in the list of products or when you click on the add to cart button it won't add the product to the cart?

Mac

YEAH

Posted: Sat Oct 05, 2002 8:39 am
by lloydsmods
It shows up in the list of products. When you click the "ADD TO CART" button, the cart opens in a seperate window but its always empty.

If you click any of the other products they will add to the cart, but not the first one in the list. And it doesnt matter how you sort. By title, by price, ascending, descending, the first item on the list won't add to the cart. Its always the first one.

Oddly enough, you can click the product name to bring up the details about the product, even if its the first on the list. You just can't add it to the cart.

Posted: Sat Oct 05, 2002 9:40 am
by twigletmac
Have you checked the HTML source to make sure that all the variables are being submitted to the form as they should?

Mac

Posted: Sat Oct 05, 2002 10:18 am
by mydimension
lloydsmods: not related to subject but thought you might want to know. when i go to your page the grey background dosen't cover the whole page and as a result the black text of your news items is lost in the black background of your page. i am using Mozilla 1.1

I got it to work...

Posted: Mon Oct 07, 2002 2:06 pm
by lloydsmods
Hey Twig, thanks for the help. I dunno why but all of a sudden it started working. Not sure what changed but something did. I have noticed that sometimes when I make changes and upload them I have to delete all my temporary internet files to get the changes to appear. Its possible this problem was fixed several days ago and I didn't notice until this weekend when I deleted the temp files.

MyDimension, sorry that your browser doesn't like my site. My web stats don't even list MoZilla as a fraction of a percentage of visitors so I've never investigated its compatibility. Thanks for the heads-up.