PHP: Handy little Snippet for Multiline Text Box

This is a quick and dirty little snippet. I had a case where I had to fill in a Subject Line and a message with only one multiline text box. It wasn’t practical to have a hard coded subject line as it would be the same for every message or in my case bug.

Here is the full code.

<?
// Get the message String
$rawString = $_POST['comments'];

// Split the string into pieces for processing
$pieces = explode("\n", $rawString);

// First element is the subject line
$subject = $pieces[0];

// Take the array, delete the first entry, So we can pass it to $message
$messagePieces = array_slice($pieces, 1);

// Replace the \n or add a <br /> if you like.
$message = implode("<br />", $messagePieces);
echo "Subject: ". $subject;
echo "<br />";
echo "Message: ". $message;
?>
<form action="<? echo $_SERVER['php_self'] ?>" method="post">
	<textarea id="comments" name="comments">Your message</textarea>
	<input name="send" type="submit" value="Send" />
</form>

Not a lot going on here. I simply took the comments and assigned them to $rawString. The new line character is \n and are going to be on every new line (when you hit return) I can use the PHP Function explode() with our $rawString variable.

$pieces = explode("\n", $rawString);

This will give us an array to work with. Our subject is now $subject = $pieces[0];. Now we need to rebuild the message but we don’t want to loop through it printing each line separately. We can use another PHP Function called array_slice(), This will cut out the first row of the array, the Subject line.

$messagePieces = array_slice($pieces, 1);

Now we cram it all back together using PHP implode(), this little guy will rebuild the array. I decided to parse it with a
tag instead of replacing the /n, you could set this to whatever you like. But in this context we are going to use the
tag so the message will display correctly as HTML.

$message = implode("<br />", $messagePieces);

One Message box and we now have our Subject line and message. You can adapt this to do many things.

Enjoy!

How to use Google HTTP Geocoding

Google Maps - Edmonton

Getting the Latitude and Longitude of an address using the Google Maps API is actually quite easy.

Google makes this process easy buy letting us interact with their API using a URI via a HTTP Request.

By using a PHP function called urlencode I take the Address values and convert spaces to underscores so your URI won’t explode and make Google angry.

Google is nice enough to give us formatted XML, KML, CSV, and even JSON. The basic URL looks something like this http://maps.google.com/maps/geo?q=address+city+state&output=xml using a plus (+) signal the next field like City, Province, or Postal Code.

Click to see output example.

In my application all my contact information is stored in a Database, when I save or update a contact I generate the URL and bring back some XML to work with.

   $googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($country)."&output=xml";

The next task was to get the contents of the file, for this I use the “file_get_contents” function and pass it my URL.

   $googlePage = file_get_contents($googleAddress);

PHP has a neat little function called SimpleXMLElement that will convert XML to an object.

Note: The SimpleXMLElement will only work in PHP 5+.

$xml = new SimpleXMLElement($googlePage);

Now that we have the values from the XML we can assign variables to them that will allow us to display the results.

http://ca.php.net/xml

list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);

At this point you can use the information and store it in a database as I did, Or just echo it out.

echo "Longitude: $longitude, Latitude: $latitude";

Here is the full code:

$address = '1 Sir Winston Churchill Square';
$city = 'edmonton';
$province = 'alberta';
$country = 'canada';
$postalcode = 'T5J 2R7';

// Google Geo Address
$googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($postalcode).'+'. urlencode($country)."&output=xml";

// Retrieve the URL contents
$googlePage = file_get_contents($googleAddress);

// Parse the returned XML file
$xml = new SimpleXMLElement($googlePage);

// Parse the coordinate string
list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);

// Output the coordinates
echo "Longitude: $longitude, Latitude: $latitude";

Links:
Google Geocoding HTTP Request

WordPress: Dealing with three levels of navigation

Path

As I mentioned in a previous post (WordPress is great but..) there are some issues that can arise when trying to use the WordPress navigation in a way that it was not intended. WordPress handles 2 levels with no issues at all, but three can be complicated.

You have your root level navigation with Home, Services, and About. Each of those pages containing sub pages for instance under Services we will have each service. Each Service has some Examples but we would like to list them in a side bar rather than horizontally in the main navigation.

WordPress would love to spit it out like this:

  • Home
  • Service
    • Service 1
      • Example a
      • Example b
      • Example c
    • Service 2
      • Example d
      • Example e
    • Service 3

WordPress only carries a parent child relationship. It is unaware of anything greater or smaller.

Meaning Example A has no idea that it is located under Services, However WordPress will render the correct CSS attributes for current_page_item, current_page_ancestor, and current_page_parent. So WordPress on some level knows how to do this but has not extended that to its navigation functions.

Since the root navigation knows where it sits and the third level of navigation will only be shown id the current page ID has child pages the issues lie in the second level, more or less the link.

So how can this be resolved? Well it will take a bunch of work, not complicated work but something that should be embedded into WordPress.

$parent_id = $post->post_parent;
$parent = get_post($parent_id);

  if($parent->post_parent){
  	$top_level_id = $parent->post_parent;
  }else if($post->post_parent){
  	$top_level_id = $post->post_parent;
  }else{
  	$top_level_id = $post->ID;
  }

  if($parent->post_parent){
  	$children = wp_list_pages("title_li=&child_of=".$parent->post_parent."&echo=0&depth=1");
  	$my_id = $parent->post_parent;
  }else if($post->post_parent){
  	$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
  	$my_id = $post->post_parent;
  }else{
  	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
  	$my_id = $post->post_parent;
  }
  if ($children) { ?>
<div id="subnavigation">
  <ul>
  	<?php echo $children; ?>
  </ul>
</div>

We basically traverse the page IDs setting when needs to be set in order to make the navigation act predictably.

PHP Frameworks – Intellisense for Aptana

This will be something that will knock your socks off!

Aptana has what is called Intellisense, this is nothing new. Many modern IDE’s have Intellisense for standard HTML, XML, CSS, JavaScript, and other languages like PHP, and Ruby.

Setting up a Library Location

This will be a place where you can store your various frameworks. I store the frameworks in there own folders separate from the production scripts for Three reasons. One, I can keep versions of my library’s. And Two so they are independent of the production application. And Three I can update all the Frameworks with SVN.

PHP Librarys

  • Cake PHP
  • CodeIgniter
  • ZendFramework
  • Symfony
  • Flourish

Configuring Aptana

I am using Aptana Studio, build: 1.5.0.025215 at the time of writing this tutorial but from the previous versions I have used the steps remain the same.

Open Aptana Studio (or Eclipse with your Aptana plug-in)

Open Aptana/Eclipse Preferences

Continue reading “PHP Frameworks – Intellisense for Aptana” »