Code Samples

Clinton is rooted in server side programming with PHP and MySQL, but is also fluent in User Interface (UI) Design with jQuery and web development standards.

  1. Here are some code samples, I hope you enjoy:
    Download all of these files
    • Basic PHP File

       specifies meta title
       * $MetaKeywords => specifies meta keywords
       * $MetaDescription => specifies meta description
       *
       * Note that if you elect to use the variable inline on a page to overwrite the
       * default values specified in the config file (brand.com/includes/config.inc.php)
       * then they will be displayed EXACTLY as you enter them (with Apollo's standard
       * SEO best practices applied).
       *
       * Last Modified: 2010-01-19: Clinton Paquin
       * Last Modified: 2010-11-14: Clinton Paquin
       ********************************************************************************/
      //Define Sitewide Meta defaults - alternatively, you can set these in a global config file (global.inc.php)
      $MetaTitle_Default = DOMAIN_NAME;
      $MetaKeywords_Default = str_replace("-", ",", $MetaTitle_Default);
      $MetaDescription_Default = "Clinton is a down to earth Web Developer with a passion for dynamic technology.";
      $MetaDescription_Default .= "He uses this site to provide details on his background, expertise, and how you can contact him.";
      
      //create array of elements to use based on vars used on every page in site
      if(strlen($section)) {
      	$MetaData[]=$section;
      }
      if(strlen($subsection)) {
      	$MetaData[]=$subsection;
      }
      if(strlen($page)) {
      	$MetaData[]=$page;
      }
      if(strlen($subpage)) {
      	$MetaData[]=$subpage;
      }
      
      if( is_array($MetaData) ) {
      	//break into a single delimited string, clean it up a little while you're at it
      	foreach($MetaData as $k=>$v) {
      		$MetaData[$k] = str_replace("-", " ", $v);
      	}
      	$MetaText = implode(" - ", $MetaData);
      	$MetaText = ucwords(strtolower($MetaText));
      }
      
      /********************************************************************************
       * Process meta title tag ...
       ********************************************************************************/
      if(strlen($MetaTitle)) {
      	//If title has been placed inline on page, use it!
      	$MetaTitle = $MetaTitle;
      } else {
      	//use a default
      	if($MetaText) {
      		$MetaTitle_Default .= " - " . $MetaText;
      	}
      	$MetaTitle = ucwords(strtolower($MetaTitle_Default));
      }
      
      /********************************************************************************
       * Process meta keywords
       * SEO best practices says to add the title to keywords, replacing "-" with ","
       ********************************************************************************/
      //If meta keywords defined inline on page then you're good to go!
      if(! strlen($MetaKeywords)) {
      	//Look to the DB for information
      	if(strlen($MetaTitle)) {
      		$MetaKeywords = $MetaTitle;
      	} else  { //should NEVER be here, but just in case - use a default definition
      		$MetaKeywords = $MetaKeywords_Default;
      	}
      }
      $MetaKeywords = str_replace(" - ", ", ", $MetaKeywords);
      
      
      /********************************************************************************
       * Process meta description
       * Apollo SEO best practices says to use only the first 200 characters
       ********************************************************************************/
       //If meta description defined inline on page then you're good to go!
      if(! strlen($MetaDescription)) {
      	//Use default definition
      	$MetaDescription = $MetaTitle . ", " . $MetaDescription_Default;
      }
      $MetaDescription = str_replace(" - ", ", ", $MetaDescription);
      $MetaDescription = substr($MetaDescription, 0, 200);
      ?>
    • Basic Class

      $Users = new Manager($mdb2, 'users_announcements', $fields);
      		$this->$Clients = new Manager($mdb2, 'clients_announcements', $fields);
       	}
      
      
       	/**
       	 * Get all announcments for specified user
       	 *
       	 * @param unknown_type $limit
       	 */
       	function GetAnnouncements($user, $limit='') {
       		if(is_numeric($user)) {
       			$Data[] = $this->GetClientAnnouncements($user, $limit);
       			$Data[] = $this->GetUserAnnouncements($user, $limit);
      
       			return $Data;
       		}
       	}
      
      
       	/**
       	 * Get announcments for specified user
       	 *
       	 * @param unknown_type $limit
       	 */
       	function GetUserAnnouncements($user, $limit='') {
       		if(is_numeric($user)) {
       			$Data = $this->Users->listContentById($user);
      
       			return $Data;
       		}
       	}
      
      
       	/**
       	 * Get announcments for specified client
       	 *
       	 * @param unknown_type $limit
       	 */
       	function GetClientAnnouncments($client, $limit='') {
       		$Clients = $this->GetClientList();
      
       		if($Clients) {
       			foreach($Clients as $k=>$v) {
       				$Data[$v['name']] = $this->Clients->listContentById($client);
       			}
       		}
      
       		return $Data;
       	}
      
      
       	/**
       	 * Get the list of clients for the specified user
       	 *
       	 * @param unknown_type $user
       	 * @return unknown
       	 */
       	function GetClientList($user) {
       		if(is_numeric($user)) {
      	 		$sql = "Select id, name from teams
      	 				Where uid='$user' and active='1'";
      	 		$Data = $this->__DB->queryAll($sql);
      
      	 		return $Data;
       		}
       	}
      }
      
    • Extended Class

      View this file in action
      db = $db;
      
      		//If someone is dumb enough to enter the ENTIRE state name (Ie. 'California' instead of 'CA'), help them out
      		$key = array_search(strtoupper($state), $states);
      		$this->search_state = $key ? $key : $state;
      		$this->search_city = $city;
      		$this->search_zipcode = $zip_code;
      		$this->search_radius = $search_radius;
      		$this->limit = $limit;
      		$this->offset = $offset;
      
      		if (!$db) {
      			return null;
      		}
      	}
      
      
      
      	/**
      	 * @return array - locations based on the search state
      	 */
      	public function get_locations_by_state() {
      		$sql = "Select * From $this->location_database_table
      				Where state = '$this->search_state'
      				Order by city ASC
      				Limit $this->limit, $this->offset";
      		$Locations = $this->db->getAll($sql,2);
      
      		return $Locations;
      	}
      
      
      	/**
      	 * @return array - locations based on the search state
      	 */
      	public function get_locations_by_city() {
      		$LatLong = $this->find_latitude_longitude_by_city();
      
      		$latitude_lookup = $LatLong['latitude'];
      		$longitude_lookup = $LatLong['longitude'];
      
      		if (strcmp($LatLong['state'],'')) {
      			$this->search_state($LatLong['state']);
      		}
      
      		//determine the range of lat/long to search
      		$range = $this->miles_to_lat_long * $this->search_radius;
      
      		$min_lat = $latitude_lookup - $range;
      		$min_lon = $longitude_lookup - $range;
      		$max_lat = $latitude_lookup + $range;
      		$max_lon = $longitude_lookup + $range;
      
      		$sql = "SELECT l.*,
      				FORMAT(ABS(POWER(($longitude_lookup - z.longitude), 2)) + ABS(POWER(($latitude_lookup - z.latitude), 2)), 2) As distance
      				FROM $this->location_database_table AS l, $this->zipcode_database_table_name As z
      				WHERE z.latitude >= $min_lat AND z.latitude <= $max_lat
      				AND z.longitude >= $min_lon AND z.longitude <= $max_lon
      				AND z.zip=l.zip";
      
      		$sql .= " Group by products_id
      				 Order By distance ASC
       				 Limit $this->limit, $this->offset";
      
      		$Data = $this->db->getAll($sql,2);
      
      		return $Data;
      	}
      
      
      	/**
      	 * @return multi-dimensional array of states
      	 */
      	public function check_for_multiple_states() {
      		$sql = "Select l.state, count(l.state)
      				From $this->zipcode_database_table_name as a, $this->location_database_table as l
      				Where z.city = '$this->search_city' AND z.city=l.city";
      
      		if (strcmp($this->search_state,'')) {
      			$sql .= " AND z.state = '$this->search_state' ";
      		}
      		$sql .= "Group By l.state";
      
      		$States = $this->db->getAll($sql,2);
      
      		//try to google geocode if no locations found
      		if (!count($States)) {
      			$LatLong = $this->geocode();
      			$States = array(true);
      		}
      
      		return $States;
      	}
      
      
      
      	/**
      	 * @return multi-dimensional array of locations
      	 */
      	public function get_locations_by_zip() {
      		$Locations = array();
      		$LatLong =  array();
      
      		//get the latitude and longitude for the zipcode
      		$LatLong = $this->find_latitude_longitude_by_zip();
      
      		//try to google geocode
      		if (!count($LatLong)) {
      			$LatLong = $this->geocode();
      		}
      
      		if (count($LatLong)) {
      			$latitude_lookup = $LatLong['latitude'];
      			$longitude_lookup = $LatLong['longitude'];
      
      			//determine the range of lat/long to search
      			$range = $this->miles_to_lat_long * $this->search_radius;
      
      			$min_lat = $latitude_lookup - $range;
      			$min_lon = $longitude_lookup - $range;
      			$max_lat = $latitude_lookup + $range;
      			$max_lon = $longitude_lookup + $range;
      
      			$sql  = "SELECT l.*,
      					FORMAT(ABS(POWER(($longitude_lookup - z.longitude), 2)) + ABS(POWER(($latitude_lookup - z.latitude), 2)), 2) as distance
      					From $this->location_database_table AS l, $this->zipcode_database_table_name as z
      					Where z.latitude >= $min_lat AND z.latitude <= $max_lat
      					AND	z.longitude >= $min_lon AND z.longitude <= $max_lon
      					AND l.zip = z.zip
      					Group By l.id
      					Order By distance ASC
      					Limit $this->limit, $this->offset";
      
      			$Locations = $this->db->getAll($sql, 2);
      
      			return $Locations;
      		}
      	}
      
      
      	/**
      	 * @return array longitude and latitude
      	 */
      	public function find_latitude_longitude_by_zip($zipcode="") {
      		$zip = $zipcode ? $zipcode : $this->search_zipcode();
      		$Data = array();
      
      		$sql = "Select latitude, longitude
      				From $this->zipcode_database_table_name
      				Where zip = '$zip'";
      
      		$res = $this->db->getRow($sql, 2);
      
      		if ($res) {
      			//set the return array since we got back a value
      			$Data['latitude'] = $res['latitude'];
      			$Data['longitude'] = $res['longitude'];
      		}
      
      		//if none of our attempts to find the zipcode in our db, close to an existing zipcode in the db, or by google maps,
      		if (! count($Data)) {
      			$subject = "Failed to find ZipCode";
      			$body = "A zipcode for USA does not have a latitude/longitude.
      Postal Code: $zip"; $this->send_notification_email($subject, $body); } return $Data; } public function find_latitude_longitude_by_city($city="") { $Data = array(); $sql = "Select AVG(latitude) AS latitude, AVG(longitude) AS longitude, state From $this->zipcode_database_table_name"; //handle query variable(s) if ($city) { $sql .= " Where city = '$city' "; } elseif( $this->search_city() ) { $sql .= " Where city = '$this->search_city' "; } if($this->search_state()) { $sql .= " AND state = '$this->search_state'"; } //finish up query $sql .= " Group By state"; $res = $this->db->getRow($sql ,2); if ($res) { //Set the return array since we got back a value $Data['latitude'] = $res['latitude']; $Data['longitude'] = $res['longitude']; $Data['state'] = $res['state']; } //if none of our attempts to find the zipcode in our db, close to an existing zipcode in the db, or by google maps, if (!count($Data)) { $subject = "Failed to find city ($this->search_city)"; $body = "A city search for $this->search_city does not have a latitude/longitude."; $this->send_notification_email($subject, $body); } //return the longitude latitude return $Data; } /** * GEOCODE via GOOGLE API * Only geocode for the United States */ public function geocode() { $Data = array(); ini_set('allow_url_fopen','true'); ini_set('allow_url_include','true'); $maps_hostmaps_host = "maps.google.com"; $base_url = "http://$maps_hostmaps_host/maps/geo?output=xml&key=$this->google_api_key"; $request_url = $base_url . "&q="; $request_url .= (strcmp($this->search_city,'')) ? urlencode($this->search_city.",$this->search_state") : ''; $request_url .= (strcmp($this->search_zipcode,'')) ? urlencode($this->search_zipcode) : ''; $request_url .= "&gl=US"; //error_log($request_url); $url = $request_url; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); $res=curl_exec($ch); curl_close($ch); $xml = simplexml_load_string(utf8_encode($res)); $status = $xml->Response->Status->code; $country_response_code = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode; /** * google maps api reference. Status Codes can tell you a lot. status code was not 200 so we got here. * http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode * 200 - success * 602 - unknown address * 620 - too many queries */ if (!strcmp($status, "200")) { if (!strcmp($country_response_code,'US')) { // Successful geocode $geocoded_state = $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName; $coordinates = $xml->Response->Placemark->Point->coordinates; $coordinatesSplit = split(",", $coordinates); $lat = mysql_real_escape_string($coordinatesSplit[1]); $lng = mysql_real_escape_string($coordinatesSplit[0]); $Data['latitude'] = $lat; $Data['longitude'] = $lng; $new_zip_code_data = array("zip" => $this->search_zipcode, "city" => $this->search_city, "state" => $geocoded_state, "latitude" => $lat, "longitude" => $lng); $added_new_zip = $this->db->autoExecute($this->zipcode_database_table_name, $new_zip_code_data); if (! $added_new_zip) { //notify the developer $subject = "New postal code failed to auto-add"; $body = "Manually insert into $this->zipcode_database_table_name:
      city: $this->search_city
      zip: $this->search_zipcode
      latitude:$lat
      longitude:$lng"; $this->send_notification_email($subject, $body); } } } else { if (!strcmp($status,'620')) { $subject = "$domain geocode approaching daily lookup limit - TURN OFF GEOCODING NOW!"; $body = "Google returned a 620 error code:
      city: $this->search_city
      state: $this->search_state
      zio: $this->search_zipcode
      Error Code: $status

      http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
      If this is 1 of 10,000 emails with the same error code then this feature should be disabled."; $this->send_notification_email($subject, $body); } } //will either return an empty array or the lat/long we want. return $Data; } /** * @param string $subject * @param string $message */ public function send_notification_email($subject, $message, $to_addresses_array = array(), $cc_addresses_array = array(), $bcc_address_array = array()) { $M = new PHPMailer(); $M->IsSMTP(); $M->IsHTML(true); $M->From = "locations@$domain"; $M->FromName = "$domain Location Finder"; $M->Subject = $subject; $M->AddReplyTo(ADMIN_EMAIL); //if they provide email addresses, then use them instead of the default array. if (count($to_addresses_array)) { $this->admin_email = $to_addresses_array; } //add email addresses foreach($this->admin_email as $temp_email) { $M->AddAddress($temp_email); } //add CCs if (count($cc_addresses_array)) { foreach($cc_addresses_array as $k=>$v) $M->AddCC($v); } //add BCCs if (count($bcc_address_array)) { foreach($bcc_address_array as $k=>$v) $M->AddBCC($v); } $M->Body = $message; $sent = $M->Send(); } /****************************************************************** * PROPERTY ACCESSORS ****************************************************************** * These functions are named EXACTLY like the fields they access * Could use MAGIC getter/setter, but playing it safe *****************************************************************/ /** * @param string */ public function search_zipcode($zipcode="") { if($zipcode) { $this->search_zipcode = $zipcode; } else { return $this->search_zipcode; } } /** * @param string */ public function search_state($state="") { if($state) { $this->search_state = $state; } else { return $this->search_state; } } /** * @param string */ public function search_city($city="") { if($city) { $this->search_city = $city; } else { return $this->search_city; } } /** * @param string */ public function search_radius($radius="") { if($radius) { $this->search_radius = $radius; } else { return $this->search_radius; } } /** * @param string */ public function limit($limit="") { if($limit) { $this->limit = $limit; } else { return $this->limit; } } /** * @param string */ public function offset($offset="") { if($offset) { $this->offset = $offset; } else { return $this->offset; } } } ?>

Need More

Can't find an example of something you'd like to see, feel free to contact me and I'll send it right over!