Debugging objects in Javascript with prototype.js

Often when coding complex javascript you’ll wish you had the ability to debug objects or arrays that you are working on – much like Data::Dumper in Perl or var_dump in php (see previous posts on these topics). Prototype.js provides an easy way to do this using the .inspect() method for hashes and arrays:

var myComplexHash = {Element: value, AnotherElement: value2 ... };


The $H(myhash).inspect() function returns a debug-oriented string which can be easily deciphered:

<#Hash:{Element: 'value', AnotherElement: 'value2', ... >

“Foreach” style loop syntax for Perl, Php and Javascript

I am always forgetting the syntax for foreach() loops in javascript. They actually don’t exist (not in their Perl or Php form anyway) and they differ in that they iterate over all the properties of an object, not an array. You can get some unexpected results if you aren’t careful.

In perl you do the following:

my @myArray = qw(One Two Three);
foreach my $item (@myArray)

    print "$item\n";

In Php you do very similar – note the “foreach (array as item)” syntax:

$myArray = Array("One", "Two", "Three");
foreach ($myArray as $item)
    print "$item\n";

In Javascript you use “for (var in object)” syntax:

var myArray = new Array("One", "Two", "Three");

for (item in myArray)

SilverStripe – An excellent Web 2.0 CMS

I’ve been evaluating a number of CMS for a client recently. These include Xoops, Joomla and now SilverStripe .
Joomla is great if you have an ‘intranet’ type site you need to build, where you need features such as Document management, News articles and advanced eCommerce functionality.

Joomla’s advantage in these cases is its massive extension repository. However, its not Web 2.0 – it feels old and clunky. Even Joomla 1.5 doesn’t fully address these issues. Installing the plugins is often extremely time consuming and causes much hair-pulling as many of them have zero documentation and some simply don’t work as advertised.

Enter SilverStripe. Developed by a group of Kiwi’s, SilverStripe is a powerful Web 2.0 based CMS system, which is based on LAMP and is extendable and fully OO.

Installation is a breeze, it includes a ever growing swathe of themes, and has fantastic drag-and-drop admin interface which I have grown to love. It has a simple (even in Beta) eCommerce module (integration with WorldPay and Payment Express), a fantastic Blog module, a great Gallery module… the list goes on. All available from the SilverStripe modules page.

If you are looking for a new CMS system which is EASY to use and EASY to install, keep your eye on SilverStripe. It’s going to be massive.

Returning an Excel file from a PHP script

I needed to return an Excel file from a PHP script I wrote.  The simplest way to do this is to create a page consisting of a single table and then setting the headers correctly.  Excel will read the HTML table and convert it automatically into an excel spreadsheet – much easier than writing out a raw Excel file!

To do this, generate your HTML for the table and place it in a variable (don’t print it):

$html = "";
$html .= "";
$html .= "";

Then print out the headers as follows:

header('Content-type: application/msexcel');	
header('Content-disposition: attachment; filename=export.xls');
print $html;

And the result will be a excel file that the browser will download and open in MS Excel!

Mysql: Defaulting a DATE field to current date

I recently came across the Mysql TIMESTAMP field type.  I’ve seen this datatype for years, however have never really used it – until now.  I’ve been looking for a way to set a fields value to the current date/time by default.  You can’t do this with DATETIME field types in mysql, however you can do it with TIMESTAMP fields:

CREATE TABLE Person_Example(
   Person_Name VARCHAR(256),  

More information is available from the mysql manual.

Problem with PHP json_decode() and prototype.js .toJSON() function

I have been tearing my hair out trying to work out why PHP’s json_decode() function wouldn’t convert a JSON string sent via AJAX using the prototype.js .toJSON() function. I was sending the string:

{\"Assembly_ID\": 1}  

However json_decode() in php would return NULL. After some fiddling around, I determined that json_decode() doesn’t like the “\” characters. Modifying the string as follows solved the problem:

{"Assembly_ID": 1}

The reason they were being sent incorrectly in the first place was because of the option in php.ini:

 magic_quotes_gpc = On

Change this setting to “Off” and it all works!

Data::Dumper for PHP

Along the same lines of my previous post, I was looking for a PHP equivalent to Data::Dumper for Perl. Php has a builtin function called var_dump() which will give you some fairly ugly output about your array/hash/object, however I was looking for something a little more elegant.Thankfully the folks over at Pear have come up with a very nice alternative. Simply install the Var_Dump PEAR module and then do:

require_once 'Var_Dump.php';


Alternative you can use the excellent built-in PHP function print_r() which does the same as Var_Dump without all the extra information included:


Equivalent to Perl’s ‘chop()’ function for php

I’ve been writing Perl code since just after I learned to walk, so I’m always looking for ways to do things in a Perl-ish manner. My latest requirement was an equivalent to the perl chop() command to remove the last character of a string for PHP. After a bit of searching around, it appears the best way to do this is:

$string = substr($string, 0, -1);

This method avoids unnecessary interpolation overheads (apparently)!

Javascript Regular Expressions

I was attempting to complete a very simply javascript regular expression which removes a few characters from a string:

my_field_124  => my_field

So i created the following javascript:

fieldName.replace (/\_\d+/, '');

What I thought would display is the string “my_field”. Instead I kept getting “my_field_1” returned. The reason for this is that the replace() function returns the resulting value, and doesn’t change the value of the original string! Change the script to:

alert(fieldName.replace(/\_\d+, '');

And it works!

Gmail Forwarding Delays, Problems when emailing from another Gmail account

I setup auto-forwarding for a client’s Gmail account today and ran into a couple of interesting problems. Firstly, I tested the auto-forwarding function from my own Gmail account, with the forwarding email address set to my own Gmail account. After a number of attempts, i discovered that i wasn’t receiving the forwarded emails.

After playing around a bit, I discovered that the forwarded emails worked fine as long as I didn’t have the forwarding address set to the same as the address I was sending the test emails from. After sending a test email from a different account, the forwarding worked fine. Lesson: don’t try and test Gmail auto-forwarding by sending an email from the address you are forwarding back to!

My columnMy column 2