Last week I began a tutorial on how to Dynamically Generate PayPayl Add to Cart or Buy Now Buttons with PHP and MySQL. Now, here is the rest.
Where we left off..
The last bit of code we had before was the following:
echo "
<table border="0" width="600">
<tbody>
<tr>
<th>Description</th>
<th>Picture</th>
<th>Price</th>
</tr>
";
while($row = mysql_fetch_array($result))
{
echo "
<tr class="\itemname\">";
echo "
<th colspan="3">" . $row['desc'] . "</th>
</tr>
";
echo "
<tr class="\imgrow\">
<td class="\imgcell\">" . "<img src="\" alt="\Description" />" . "</td>
";
echo "
<td class="\pricecell\">" . "$" . $row['price'] . "
plus Shipping" . "</td>
";
This will generate an HTML table that has a Table Row for each Row in your MySQL table. This will display the Item description, a picture of the item located in images/products (Make sure that your item name and picture name are the same), and a table cell with the price. These cells also have different classes applied so you can style them with CSS however you need to. echo can print the html and call the MySQL table to present info when requested. I could explain this more in depth, however, there are so many PHP Tutorials out there, I’m sure you can find something better than I can write.
Now that we are connected and our HTML table is set, we need to Call our product information from the MySQL Table. Let’s add the PayPal Add to Cart Button to our current table. We will have Dynamically Generated Fields for each product.
The Code
<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=\"[email protected]\" />
<input type=\"hidden\" name=\"lc\" value=\"US\">
<input type=\"hidden\" name=\"item_name\" value=\"" . $row['desc'] . "\" />
<input type=\"hidden\" name=\"item_number\" value=\"" . $row['id'] . "\" />
<input type=\"hidden\" name=\"amount\" value=\"" . $row['price'] . "\" />
<input type=\"hidden\" name=\"currency_code\" value=\"USD\" />
<input type=\"hidden\" name=\"button_subtype\" value=\"products\" />
<input type=\"hidden\" name=\"shipping\" value=\"0\" />
<input type=\"hidden\" name=\"handling_cart\" value=\"3.95\" />
<input type=\"hidden\" name=\"add\" value=\"1\" />
<input type=\"hidden\" name=\"bn\" value=\"PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest\" />
<input type=\"image\" src=\"images/addcart.gif\" border=\"0\" name=\"submit\" alt=\"PayPal - The safer, easier way to pay online!\" />
Some of the changes you’ll see, are all the slashes again. This is simply to allow the quotation marks to appear, and not break the PHP. You’ll notice most of the values are the same, except a few.
<input type=\"hidden\" name=\"item_name\" value=\"" . $row['desc'] . "\" /> The value $row['desc'] is the most important part here, and is pretty straightforward. This part calls the ‘desc’ row from the MySQL database.
<input type=\"hidden\" name=\"item_number\" value=\"" . $row['id'] . "\" /> Each item in your database will need to have a unique ID, this will identify them in your transactions.
<input type=\"hidden\" name=\"amount\" value=\"" . $row['price'] . "\" /> Again, this is pretty straightforward, but this will display the price column for each item.
The rest of the code is the same as the PayPal Button above, the standard one. The only other addition is the \’s.
That’s It!
Hopefully this helps someone who has the same problem as I did. In retrospect it is fairly easy, but while trying to figure things out it can be overwhelming.
Let me know what you think, and feel free to leave any feedback, whether positive or negative.
I just scanned your two articles on Paypal add-to-cart buttons but it appears to be just what I’m looking for. We were manually creating the buttons through the Paypal site, but now have way too many products. The only thing that scares me about the dynamic generation is security. Paypal says a lot to encourage hosted buttons, and use of Button Manager API to create them … which is definitely not clear. I’m debating the security aspect of clear button generation. Any thoughts?
Thanks for your excellent articles. The only clear ones I’ve seen after lots of looking!