Notes & Code Snippets

Read notes about design & development, or useful scripts and bits of code from me

Internet Explorer reminder

January 05, 2009

Keywords: IE internet explorer problem

When working with Javascript files in Internet Explorer, if IE finds a syntax error anywhere in an external .js file, instead of giving you an error message telling you there's an error, it will simply disregard ALL code in the ENTIRE .js file.

The only error message it gives you is a notice that, whenever you try to use a function located in the offending .js file, the function does not exist.

Thank you, Microsoft, for this wonderful feature -_-

Anyway, note to self: Putting a comma after the end of a comma-separated list (in my case, leaving a comma at the end of the last item in a list of options for a MooTools class), will be considered a syntax error in IE. Both Firefox and Safari have no problem with it. (not to mention PHP).

Back to top

Javascript print_r equivalent

December 29, 2008

Keywords: javascript java script print_r printr php equivalent

Useful Javascript function that mimics PHP's print_r function.
function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>[" p "] => " typeof(theObj) "</li>");
      } else {
document.write("<li>[" p "] => " theObj[p] "</li>");

Back to top

Find a specific file in a specified directory

December 10, 2008

Keywords: php files directory find file

Locates the first file with a name matching the passed string and returns its path relative to the path passed You can also pass it an array, and it will search the array for a value matching the $file. If you pass an extension, it will only look for files with that ext
function findFile($file, $dir, $ext=''){
		$path = $dir;
		$dir = dirList($dir);
	foreach($dir as $k=>$v){
		if(is_array($v) && !$location){
			$x = findFile($file, $v);
			if($x)$location .= $k.'/'.$x;
		else if($v == $file && !$location)$location .= $v;
	if($location)return $location;

Back to top

Create an array of all files within a specified directory

December 10, 2008

Keywords: php files array directory

Creates a multidimensional array of your entire folder and file structure starting from the directory it is passed. Keys for files are in numeric order, while keys for subdirectories are the name of the directory
function dirList ($directory){
	// create an array to hold directory list
	$results = array();
	// create a handler for the directory
	$handler = opendir($directory);
	// keep going until all files in directory have been read
	while ($file = readdir($handler)) {
	// if $file isn't this directory or its parent,
	// add it to the results array
	if ($file != '.' && $file != '..'){
		if(is_dir($directory.$file))$results[$file] = dirList($directory.$file);
		else $results[] = $file;
	// tidy up: close the handler
	// done!
	return $results;

Back to top

Generate PHP code to recreate an array

December 10, 2008

Keywords: php array generator

If you've ever had an array dynamically generated and wanted a way to hardcode the same array, this function will take an array and generate the PHP code necessary to rebuild it.
function php_for_array($array){
		$string .= "array(\n";
		foreach($array as $k=>$v){
			if(is_array($v))$string .= $sep.'"'.$k.'"	=>	'.php_for_array($v);
			else $string .= $sep.'"'.$k.'"	=>	"'.$v.'"';
			$sep = ",\n";
		$string .= "\n)";
	return $string;

Back to top

1 2 3