Home
Our Services
Our Clients
Code Snippets
Contact Us
Privacy Policy


 

Free PHP Code Snippets

At Blue Sky iSolutions, we develop solutions where none exist, or where existing solutions are over-priced and/or under-performing. We decided to make some of our more useful PHP functions available FREE to use by other developers. Please feel free to use any of the code snippets below for FREE, but without any warranty whatsoever.

PayPal Payments Pro Plugin For Wordpress
Wordpress Paypal Pro Plugin Admin Panel Our PayPal Payments Pro Wordpress plugin provides a means to integrate PayPal Payments Pro payment processing into your Wordpress installation. The PayPal Payments Pro plugin Admin panel provides the ability to set the required PayPal API credentials as well as the amount to charge, the title of the payment form, the subject of the email notification sent to the buyer, the FROM email address of the email notification sent to the buyer, as well as an additional email address where the email notification may be sent automatically (in case you'd like a copy of the receipt emailed to you).

Installing the PayPal Payments Pro Wordpress plugin is easy:
1. Download and unzip the plugin.
2. Move or copy the paypal-pro directory into your Wordpress plugins directory, usually at /wp-content/plugins/
3. Login to Wordpress as Admin and turn on the PayPal Pro plugin.
4. Set up your PayPal API Credentials in Admin, under Settings > PayPalPro (see picture on the right).
5. Place the plugin code in your templates, wherever you'd like the payment form to appear, using the ppp_form action, example follows.
<?php do_action('ppp_form'); ?>

Note: The PayPal Payments Pro Wordpress plugin requires that you have a business account with PayPal, after which you will have the ability to request and access your PayPal API Credentials.

Download the PayPal Pro Plugin for Wordpress

See the PayPal Pro Plugin for Wordpress Installed. (PayPal test credit card required to complete payment.)

PHP Utility Functions
Sometimes there "must" be a function that does "that" already. But sometimes there isn't. Enough said. Following are some utility PHP functions we use all of the time.

PHP function sqlSafe: This function returns the parameter string after applying stripslashes and mysql_real_escape_string to the string. We use this when magik quotes is turned on, to clean POST/GET input before input into a database.

function sqlSafe($in_string) {
     return mysql_real_escape_string(stripslashes($in_string));
}

PHP function htmlSafe: This function returns the parameter string after applying stripslashes and htmlspecialchars with ENT_QUOTES to the string. We use this when magik quotes is turned on, to clean POST/GET input before display within HTML forms.

We suggest that you always single-quote parameters in html tags and consitently use ENT_QUOTES for the htmlspecialchars() function. This function assumes that you follow this practice. If you prefer to double-quote HTML parameters, change ENT_QUOTES to ENT_NOQUOTES. If you vary single and double quotes around HTML parameters, don't use this function.

function htmlSafe($in_string) {
     return htmlspecialchars(stripslashes($in_string), ENT_QUOTES);
}

PHP function validEmail: This function returns true or false depending on whether the parameter string is a valid email address (true) or not (false).

function validEmail($email) {
     if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$",$email)) {
          return true;
     } else {
          return false;
     }
}

PHP function formSelect: This function returns an HTML form <select> element filled with the options provided, with an option selected (or not), and including the ability to add Javascript and/or style information to the <select> element. This function uses the htmlSafe function, above. The parameters for this function are as follows:

$var_name - Value for the "name" parameter of the <select> element
$selected - Value of the selected option, if any
$options - The array to use for the options in the <select> element
$empty - An optional additional element to add at the top of the option list, for instance "Select One" or a similar prompt
$jscript - Optional javascript to include in the <select> element, for instance to handle an onChange event
$style - CSS descriptors for the style of the <select> element. Do NOT include the "style=" portion, as that is added automatically.

function formSelect($var_name, $selected, $options, $empty = "", $jscript="", $style="") {
     // Create and return an HTML <select>
     if ($jscript > "") { $jscript = " $jscript "; }
     if ($style > "") { $style = " style='$style'"; }

     $select = "<select name='$var_name'$jscript$style>\n";

     if ($empty > "") {
          $select .= "<option value=''>$empty</option>\n";
     }

     foreach($options AS $value=>$option) {
          $select .= "<option value='$value'";

          if (is_array($selected)) {
               if (in_array($value,$selected)) {
                    $select .= " SELECTED";
               }
          } else {
               if ($value == $selected) {
                    $select .= " SELECTED";
               }
          }

          $select .= ">".htmlSafe($option)."\n";
     }

     $select .= "</select>\n";
     return $select;
}

Example usage of function formSelect to create a select for credit card type:

<php
// Create an array of credit card types:
$credit_cards = array(1=>"Visa",2=>"MasterCard",3=>"Discover",4=>"American Express");

// Create the select element with the name creditCartType,
// with "Discover" selected, an empty option,
// and with some javascript and styling thrown in:
echo formSelect("creditCartType", 3, $credit_cards, "Select a Card",
     "onChange='alert(\"Hello World\");'", "color:blue;font-size:24px;");
?>

Example output of function formSelect:

 

PHP Function NewLatLong: This function returns an array of 2 elements: the new latitude and longitude after moving from an originating latitude/longitude to a new position at a given bearing and range. Use this function to determine the new latitude and longitude after moving a known bearing (direction) and range (distance). The parameters for this function are as follows:

$lat1 - Start latitude in decimal degrees
$lon1 - Start longitude in decimal degrees
$bearing - Bearing in decimal degrees
$distance1 - Distance in nm
$projection - Map projection, optional, default 1 (sphere). One of:
1 = sphere (1' = 1nm)
2 = WGS84
3 = NAD27
4 = International
5 = Krasovsky
6 = Bessel
7 = WGS72
8 = WGS66
9 = FAI sphere

Function NewLatLong() is large and uses several utility functions. The function and the required utility functions are available for download using the link below:

Download PHP function NewLatLong() and supporting functions.

 


Copyright © 2003 - 2010, Blue Sky iSolutions