PayPal Hacks. 100 Industrial-Strength Tips and Tools Free Open Book

PayPal Hacks. 100 Industrial-Strength Tips and Tools

Previous Section  < Day Day Up >  Next Section

Hack 54 Create a Dynamic Storefront

figs/moderate.gif figs/hack54.gif

Produce a powerful storefront with a simple database and dynamic server scripting.

PayPal's Button Factory makes managing a small web store easy, provided that you have a small number of products. But if your store has hundreds or thousands of products, generating the necessary HTML code through the Button Factory (not to mention later changing that code) would be a daunting task. Therefore, you'll need a method to quickly generate generic shopping cart HTML button code for all your store's products.

This hack provides an ideal situation for a database-driven page that can use a single page as a template for an arbitrary number of products contained in a database. The example illustrates the techniques using Microsoft Active Server Pages written in VBScript with an Access database, though the principles described here can be applied to any server platform/database combination.

5.11.1 Creating the Storefront Database

The first step in building your dynamic site is to create a database table that holds your PayPal button values for all your products. You'll need one column for each unique aspect of the button for each product: item_name, item_number, item_price, and Id. Both item_name and item_number should be text fields, while item_price should be a money (or currency) field. Finally, include the Id field as the primary key and set it to increment automatically.

Save this new database table as tblProducts, as shown in Figure 5-7. Your table can have more rows, including shipping information, return URLs, or tax data, depending on the variables you are using for your buttons.

Figure 5-7. The database table containing your product information
figs/pph_0507.gif


See the "Database Coding and Platform Choices" section of the Preface for database considerations.


Once the table is built and saved, populate it with your product data. You can enter the information into the table like a spreadsheet or import the data from another source. After the data is entered, your database is ready for use in your dynamic page.

5.11.2 Building the Template

The second step in creating your storefront is to generate generic HTML Button Factory code [Hack #28] to serve as your template for your database-driven store. Your button code should look something like this:

<form target="paypal" action=

                "https://www.paypal.com/cgi-bin/webscr" method="post">

<input type="hidden" name="business" value="yyouremail@yyourisp.com">

<input type="hidden" name="item_name" value="Widget">

<input type="hidden" name="item_number" value="Wid-001">

<input type="hidden" name="amount" value="1.00">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="image" src=

                "https://www.paypal.com/en_US/i/btn/x-click-but22.gif" border="0"

                name="submit">

<input type="hidden" name="add" value="1">

</form>

The storefront page displays all the items for sale by taking the information in your tblProducts database table and dynamically inserting it into the generic PayPal Button Factory code you just created. To get started, use a SQL query to retrieve the product information. Depending on the server platform, languages supported, and database technology used, the syntax to connect to the database and return the data will vary. The SQL query to create your recordset should look like this:

SELECT item_name, item_number, item_price, Id FROM tblProducts

See the "Database Coding and Platform Choices" section of the Preface for the additional information needed to put this SQL statement to work with this and the other hacks in this book.


Your database then returns all of the products in the table, which you'll need to place into a recordset called rsProducts.

Next, take the generic button code from the previous step and replace the field values with references to fields in your database. For instance, change this line:

<input type="hidden" name="item_name" value="Widget">

to this (assuming you're using VBScript for ASP, as discussed in the Preface):

<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">

Your final code should look something like this:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"

                method="post">

<input type="hidden" name="business" value="yyouremail@yyourisp.com">

<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">

<input type="hidden" name="item_number" value="<%=rsProducts

                ("item_number")%>">

<input type="hidden" name="amount" value="<%=rsProducts("item_price")%>">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif"

                border="0" name="submit">

<input type="hidden" name="add" value="1">

</form>

When this page is loaded into a web browser, your server executes the SQL query before it is presented to the customer. The code then pulls the first item from the recordset and generates the button code for the corresponding product dynamically. The next step is to generate a whole page of buttons, one for each item in your database:

'While recordset still has products, loop code

While NOT rsProducts.EOF



<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"

                method="post">

<input type="hidden" name="business" value="yyouremail@yyourisp.com">

<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">

<input type="hidden" name="item_number" 

                value="<%=rsProducts("item_number")%>">

<input type="hidden" name="amount" value="<%=rsProducts("item_price")%>">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif"

                border="0" name="submit">

<input type="hidden" name="add" value="1">

</form>



'Move to next record

rsProducts.MoveNext( )

Wend

Figure 5-8 shows the finished product listing, complete with multiple dynamically generated payment buttons.

Figure 5-8. The finished web page, loaded into a browser
figs/pph_0508.gif


5.11.3 Including Product Details

Not only can you use the values returned from the database to populate your button code, you can also display the item name and price (and perhaps a photo) of the product alongside each button. Add a little spacing to the buttons to keep the site organized:

'While recordset still has products, loop code

While NOT rsProducts.EOF



Product: <%=rsProduct("item_name"%><br>

Price: <%=rsProduct("item_price"%><br>

Click the button below to Buy<br>

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"

                method="post">

<input type="hidden" name="business" value="yyouremail@yyourisp.com">

<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">

<input type="hidden" name="item_number" 

                value="<%=rsProducts("item_number")%>">

<input type="hidden" name="amount" value="<%=rsProducts("item_price")%>">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif"

                border="0" name="submit">

<input type="hidden" name="add" value="1">

</form>

<br><br>



'Move to next record

rsProducts.MoveNext( )

Yound

This simple technique can serve as the foundation for a powerful eCommerce web site. Simply by managing this one template page and a database table, you can build a site that supports an arbitrary number of products, without needing to manually create and edit individual product pages.

    Previous Section  < Day Day Up >  Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


         Main Menu
    PayPal Hacks
    Table of Contents
    Copyright
    Credits
    Preface
    Chapter 1. Account Management
    Chapter 2. Making Payments
    Chapter 3. Selling with PayPal
    Chapter 4. Payment Buttons
    Chapter 5. Storefronts and Shopping Carts
    Introduction: Hacks #45-50
    Hack 45 Hack Shopping Cart Buttons
    Hack 46 Create Shopping Cart Links
    Hack 47 Specify the Size of the Shopping Cart Window
    Hack 48 Deal with Design and Layout Issues
    Hack 49 Put Both Cart Buttons in One Form
    Hack 50 Integrate a Third-Party Shopping Cart with PayPal
    Hack 51 Customize Checkout Pages
    Hack 52 Display the Merchant Transaction ID on Your Return Page
    Hack 53 Remember Your Customers
    Hack 54 Create a Dynamic Storefront
    Hack 55 Add Dynamic Storefront Details
    Hack 56 Insert Dynamic Images
    Hack 57 Build an Order-Tracking Page
    Hack 58 Offer Discount Coupons
    Hack 59 Increase Search Engine Exposure
    Hack 60 Sell Digital Goods with PayLoadz
    Chapter 6. Managing Subscriptions
    Chapter 7. IPN and PDT
    Chapter 8. The PayPal Web Services API
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele