PHP

SEARCH

About There4

I build simple, clean, and maintainable internet applications. People should never need an owners manual to use software, I believe that applications can be easy to use while still being powerful. Applications should remain valuable through their entire lifespans, and should be flexible enough to grow and change as requirements shift.

I prefer to develop with Blueprint CSS, jQuery, and Cake PHP. I have extensive development history with PHP, MySQL, Microsoft SQL, and mixed Win32/Linux hosting environments.

Follow me on Twitter

Posting tweet...

pChart and jQuery image map hover

Posted in: Blog, PHP by Craig Davis on June 19, 2010 | No Comments

I have used pChart as a graphing and charting package in several projects. It produces clean graphs, but its native tooltip support has always disappointed me.

I set about to update this with new functionality and to replace the existing javascript with something new from jQuery. I’ve chosen to use qTip to handle the tooltip production, and have extended pChart with new functions for both pChart_Map::SaveImageMap() and pChart_Map::GetImageMap(). These functions now produce an image map rather than the pChart data format.

Demo

You can see a working demo in my project sandbox.

Download

Download pChart_Map

How it works

  1. The page has a new jQuery plugin called pChart() for each of the pChart images.
  2. The browser calls the pChart image producer with a unique ID for the image
  3. As the pChart image is created, pChart writes an image map file for this ID to a tmp directory
  4. The jQuery load() event catches when pChart has loaded the image
  5. jQuery loads the newly created image map file from disk via pChart
  6. jQuery attaches the image map to the image, and then applies qTip to the chart for this image map

Notes

  • Make sure that the image tag has an id that matches the MapID on the get request for the chart.

Geolocation services with jQuery and IPInfoDB

Posted in: Blog, JavaScript, PHP by Craig Davis on March 18, 2010 | No Comments

I’m releasing a simple script that uses jQuery and a small proxy script to deliver location services via jQuery. You can see a simple example.

Geolocation with IPInfoDB

Requirements

Code and explanations

By placing this at the jQuery level it prevents any page blocking that may occur during loading, and the proxy script prevents any cross site scripting problems.

First, the javascript variables , with a small bit of PHP:

var ip = "<?php print $_SERVER['REMOTE_ADDR'];?>";
var geolocation = "./ip_proxy.php";
var cookiename = 'geo_location';
var cookieoptions = { path: '/', expires: 10 };

These variables setup the path to the proxy script, place the users IP address into a javascript variable, and establish our cookie parameters. Next, we setup our page initialization function:

$(function(){

  fetchlocation();

});
displaylocation = function(location) {
  if (location.Status == 'OK') {
    $('.city').text(location.City);
    $('.zip').text(location.ZipPostalCode);
    if(location.State != '--') {
      $('.state').text(location.State);
    } else {
      $('.state').text(location.RegionName);
    }
  }
}

fetchlocation = function() {
  // look in the cookie for the location data
  cookiedata = $.cookie(cookiename);
  if ('' != cookiedata) {
    locationinfo = $.evalJSON(cookiedata);
    if ((locationinfo != null) && (locationinfo.IP == ip)) {
      displaylocation(locationinfo);
      $.cookie(cookiename, cookiedata, cookieoptions);
      return;
    }
  }
  // it's not in the cookie, so fetch from the server
  $.getJSON(
    geolocation, {
      'timezone' : 'false', // set this to false to save the service 2 queries
      'ip'       : ip
    },
    function(data) {
      data.IP = ip;
      displaylocation(data);
      cookiedata = $.toJSON(data);
      $.cookie(cookiename, cookiedata, cookieoptions);
    }
  );
}

PHP code

The PHP code is well documented within the ip_proxy.php file. It acts as an intermediary between the jQuery script, and the IP producer. Note that the file can restrict based on hosts, and adds an extra State abbreviation variable to the data returned by the IPInfoDB API

Commit integration from bitbucket.org to Campfire chat

Posted in: Blog, PHP by Craig Davis on November 25, 2009 | No Comments

My full time employer recently began to host our projects with Bitbucket.org. I’ve been very impressed so far, and have been enjoying the ease of browsing past revisions and merges. We also use the 37 Signals collection of applications. Bitbucket has some rudimentary Basecamp integration, but we spend time collaborating in Campfire, and I wanted to be alerted when commits were made via chat.

I found a php class called Icecube that integrates with the Campfire API. The documentation for the Bitbucket post data can be found in their help docs for the post payload. I combined this class with a code found at 61924.nl, The script from 61924 can receive the payload from Bitbucket, and process it correctly. I’ve modified the script to present the commit messages with improved formatting, and have added some config options.

Download the scripts

Download Icecube at 61924.nl
Download Bitbucket and Campfire script

Setup the scripts

Bitbucket script config

Place both of the scripts into a web accessible directory, and open bitbucket.php and edit the config section of the file.

Bitbucket Post Setup
Next, open your bitbucket.org repo, choose admin, services, and then add a new post type. Then specify the url to bitbucketchat.php.

Using the script

Join the chat with another account, and make a post!

Questions or suggestions? Leave a comment!

Smarty template function for the data uri format

Posted in: Blog, PHP by Craig Davis on February 12, 2009 | 2 Comments

In a recent project, I needed to provide an html document that could be used offline. After initially experimenting with zip files and other methods of delivering the associated image files, I chose to use the somewhat rare method of using the data uri scheme. This provides us with the ability to place the image data in-line with the page.

Download the modifier

The approach is straightforward. We must base64 encode the image data and place it with the proper formatting into the src attribute of an img tag. The processing is accomplished in this case with a smarty modifier.

<img src="{img2data path='/images/inline.gif'}" />