GBIS751 Final Project - Helpful Code

This document contains the PHP code that produces a product table for the North Shore Choral Society.  This is more complicated that you need for your project but it does show how to create buttons that link to PayPal.  The code below is from the page sales/ticketord3.php.  To reach this page on the live web site, go to http://www.northshorechoral.org,then click on Tickets, then click on “Order tickets by shopping directly on our web site".

To see a version of this information in a better text format for copying, click here

This first file is a small include file called salesvars.inc
// Sales variables used to communicate with PayPal
<?php
$paypal["action"] = "https://www.paypal.com/cgi-bin/webscr";
$paypal["business"] = "orders@northshorechoral.com";
$paypal["add_button"] = "https://www.paypal.com/en_US/i/btn/x-click-but22.gif";
$paypal["view_button"] = "https://www.paypal.com/en_US/i/btn/view_cart.gif";
$paypal["pixel"] = "https://www.paypal.com/en_US/i/scr/pixel.gif";
?>
The second file is the relevant PHP code from file sales/ticketord3.php.  I have added additional comments in bold text.  These comments are not in the original code
<?php
// The code is divided into several functions.  This first function is called from the
// main code.  The calling statement looks like this:
//
//<table width="90%" border="0" cellpadding="5" cellspacing="0">
//                                            <?php
//                                            output_ticket_table();
//                                            ?>         
//          </table>
function output_ticket_table()
{
/* This processing reads the product (ticket) information from the NSCS database */
/* For each item, */
/*           It displays that information and provides PayPal "Add to Cart" and "View Cart" */
// You already have includes and a database connection so you should not need these statements
include '../include/db.inc';
include '../include/error.inc';
include '../include/clean.inc';
include '../include/salesvars.inc';

$connection = $con1;
if(!mysql_select_db($databaseName,$connection)) showerror();
// The table header is generated here
echo "<tr><td><b>Type of Ticket</b></td><td><b>Item ID</b></td><td><b>Unit Price</b></td><td><b>Select this ticket</b></td><td><b>View Shopping Cart</b></td></b></tr>";

//This is special logic to accommodate the special needs of the Executive Director
// You should not need any conditional logic around your SELECT statement

if ($_REQUEST['secret'] == "len")
{
// Show all products for any user that reaches this page with the secret parameter specified
// Any user going to http://www.northshorechoral.org/showalltickets
//   will be redirected to this page with the secret parameter set.             
                $query = "SELECT proddescr, prodprice, proddisplcode
                                                                                                FROM product
                                                                                                ORDER BY proddisplcode";
}
else
{
// Show only products currently on sale
                $query = "SELECT proddescr, prodprice, proddisplcode
                                                                                                FROM product
                                                                                                WHERE grp_id = 1
                                                                                                ORDER BY proddisplcode";
}
if (!($result = @ mysql_query($query, $connection))) showerror();

// I used a different retrieval method so I could use the variable names from
// my database such as $row[“proddescr”]  instead of ($row[0], $row[1], etc.)
while ($row = @ mysql_fetch_array($result)) {

                /* Store SQL results is easy-to-spell variable names  */
                $item_desc = $row["proddescr"] ;
                $item_no = $row["proddisplcode"];
                $item_price = $row["prodprice"];

                /* Output Type of Ticket in left side of display table */
                $buffer = "\r\n\r\n<tr><td>";
                $buffer .=  $item_desc;
                $buffer .=  "</td>";
                echo $buffer; /* Type of Ticket */

                /* Output Item Number */
                $buffer = "<td>";
                $buffer .= $item_no;
                $buffer .="</td>";
                echo $buffer; /* Item Number  */

                /* Output Item Price */
                echo "<td>$item_price</td>"; /* Unit Price */
// Note that this is complicated enough to use a separate function that is coded later
//  The name of the function is output_paypal_add_to_cart
                /* Output the Add to Cart Button */
                $buffer = "<td>";
                $buffer .= output_paypal_add_to_cart($paypal, $item_desc, $item_no, $item_price);
                $buffer .= "</td>";
                echo $buffer; /* Add to Cart */

                /* Output the View Cart Button */
                $buffer = "<td>";
                $buffer .= output_paypal_view_cart($paypal, $item_desc, $item_no, $item_price);
                $buffer .= "</td>";
                echo $buffer; /* View Cart */

 

                /* End of table row */
                echo "</tr>";
                }
                                                               
}

// This code could have been placed in-line
// I put it in a separate function to make the main-line code more readable.
function output_paypal_add_to_cart($paypal,$item_desc, $item_no, $item_price)
{

                /* Generate the Add to Cart form for this item */                           
                extract($paypal,EXTR_PREFIX_ALL,"paypal");
// Remember from the file salesvars.in that
// $paypal["business"] = "orders@northshorechoral.com";
//  The PHP extract function makes this information available in the variable “$paypal_business”

                $buffer = "<form target=\"paypal\" action=\"$paypal_action\" method=\"post\">";
                $buffer .= "<input type=\"image\" src=\"$paypal_add_button\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - its fast, free and secure!\">";
                $buffer .= "<img alt=\"$paypal_pixel\" width=\"1\" height=\"1\">";
                $buffer .= '<input type="hidden" name="add" value="1">';
                $buffer .= '<input type="hidden" name="cmd" value="_cart">';
                $buffer .= "<input type=\"hidden\" name=\"business\" value=\"$paypal_business\">";
                $buffer .= "<input type=\"hidden\" name=\"item_name\" value=\"$item_desc\">";
                $buffer .= "<input type=\"hidden\" name=\"item_number\" value=\"$item_no\">";
                $buffer .= "<input type=\"hidden\" name=\"amount\" value=\"$item_price\">";
                $buffer .= '<input type="hidden" name="no_shipping" value="2">';
                $buffer .= '<input type="hidden" name="no_note" value="1">';
                $buffer .= '<input type="hidden" name="currency_code" value="USD">';
                $buffer .= '<input type="hidden" name="currency_code" value="USD">';
                $buffer .= '<input type="hidden" name="bn" value="PP-ShopCartBF">';
                $buffer .= "</form>";
                return $buffer;

}
function output_paypal_view_cart($paypal,$item_desc, $item_no, $item_price)
{
               
                /* Generate the View Cart form for this item */
                extract($paypal,EXTR_PREFIX_ALL,"paypal");
                $buffer = "<form target=\"paypal\" action=\"$paypal_action\" method=\"post\">";
                $buffer .= '<input type="hidden" name="cmd" value="_cart">';
                $buffer .= "<input type=\"hidden\" name=\"business\" value=\"$paypal_business\">";
                $buffer .= "<input type=\"image\" src=\"$paypal_view_button\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\">";
                $buffer .= '<input type="hidden" name="display" value="1">';
                $buffer .= '</form>';
               
               
                return $buffer;
}

?>