Archive for October, 2009



Oct 16th

PHP Class Autoloader Function

Anyone who has done serious work with PHP since 5.0 knows the way to stay future proof is to adopt and love like it was your own the object oriented programming model. This little snippet has gone a LONG way to making my life easier. To use it, simply include it in your globally included functions library (yes, functions not objects.. regular every day function methods).

In the snippet below there is a constant “PATH_CLASSES”, in my global definitions I have this defined as the absolute path to my class libraries which are all in the same directory for this purpose.

function __autoload( $class_name )
{
	$file_formats = array('%s.php', '%s.class.php', 'class.%s.php', '%s.inc.php');
	foreach ( $file_formats as $file_format )
	{
		$file = PATH_CLASSES.sprintf($file_format, strtolower($class_name));
		if ( file_exists($file) ) require_once($file);
	}
	return;
}
Oct 16th

PHP Search an array of objects for a property value

While developing a new project management application I needed a way to search an array of objects for a specific object property value. For example, I had an array of “employee” objects that contain a great many properties to describe each “employee” and you want to search this array for an employee object that has a specific value for one of it’s properties. To solve this I have come up with the following little snippet:

function search_object_array($needle_key, $needle_val, $haystack)
{
	// iterate through our haystack
	for ( $i = 0; $i < count($haystack); $i++ )
	{
		// ensure this array element is an object and has a key that matches our needle's key
		if ( is_object($haystack[$i]) and property_exists($haystack[$i], $needle_key) )
		{
			// determine if comparison is case sensitive
			if ( strtolower($needle_val) == strtolower($haystack[$i]->$needle_key) ) return $i;
		}
	}
	// no match found
	return false;
}

This function returns the index of the object array if a match is found and false if no matches are found.

So lets test it.
First I’ll define a class that we will use to generate our objects that will populate our array, well call this class “foo”.

class foo
{
	public $id;
	public $name;
	public $description;

	public function __construct( $id, $name, $description )
	{
		$this->id = $id;
		$this->name = $name;
		$this->description = $description;
	}
}

Next we’ll populate our object array “$obj_arr” with objects

$obj_arr[0] = new foo(1, 'Joe', 'Joes description');
$obj_arr[1] = new foo(2, 'Sara', 'Saras description');
$obj_arr[2] = new foo(3, 'Adam', 'Adams description');
$obj_arr[3] = new foo(4, 'Jake', 'Jakes description');
$obj_arr[4] = new foo(5, 'Sally', 'Sallys description');
$obj_arr[5] = new foo(6, 'Paul', 'Pauls description');
$obj_arr[6] = new foo(7, 'Freddie', 'Freddies description');

Now lets search our new array of objects for an object that matches our criteria:

$result = search_object_array('name', 'sally', $obj_arr);
echo $result;
// will print: 4

I hope this helps!

Oct 15th

MySQL Select the first and last ID from a table in a single select statement

Just a very quick little snippet that will allow you to SELECT the lowest and highest value from a single field in a database. For example, in a recent project I had to ensure that I was not deleting the first or last record status history record for a given project in a database table. To do this I came up with the following:

SELECT
	MIN(status_history_id) as first_id,
	MAX(status_history_id) as last_id
FROM table_status_history
WHERE ( project_id = "some_unique_id" )
GROUP BY project_id;

Essentially, this selects all status history records from the table for a specific project, then we group them all together and select only the max and min from the ID field and return those as (2) ad-hoc fields.

Oct 15th

JQuery UI Dialog not showing up twice in IE ( invalid argument )

I personally love the JQuery library, it makes life soo much easier. Add to that the JQuery UI libraries and you have yourself a seriously mean platform to build killer applications. That said, I ran into a mean little bug that affects the way the UI Dialog shows up on the page in Internet Exploder, I know… stop rolling your eyes it is here to stay and we will need to learn to play nice.

The bug:
Once a JQuery UI dialog has been initialized, opened once, and then closed it cannot be reopened in IE due to an “invalid argument” error.

The fix:
Open up your copy of jquery.js (you do have it locally right?, latency alert!) around line 1060 look for:

if ( set )
			elem[ name ] = value;

and change it to:

if ( set )
			try{elem[ name ] = value;} catch (error){};

Viola!