The title probably wouldn’t make a great deal of sense under normal circumstances… but I am reffering to Coldfusion struct keys and maintaing their case.

In some circumstances it is important to make sure that the case that you enter your struct keys with is not altered, and you are probably thinking that surely whatever case you use it is maintained, but this is only in true under some circumstances. One of my amazing colleagues in our development studio has a situation where the case must be maintained at lower case converting a struct to a JSON packet to be consumed by a facebook application, but because of the way she was building the struct, CF forces the keys to upper case. Check out the example below:

<cfset stPerson = structNew()>
<cfset stPerson.name = "Sandy">
<cfset stPerson.address = "12 Amazing Street">
<cfset stPerson.suburb = "Amazingville">
<cfset stPerson.state = "SA">
<cfset stPerson.phone = "54321234">
<cfdump var="#stPerson#">

and the result:

struct
ADDRESS 12 Amazing Street
NAME Sandy
PHONE 54321234
STATE SA
SUBURB Amazingville

Check out the keys, they are all forced to upper case! Why? , well I found this on another CF developer blog and it sounded pretty good – “ColdFusion internally represents structure keys in uppercase when the keys are created using dot notation. Dot notation is typically how programmers write their code and is considered to be best practice when working with structures.”

So there you go…. but how can we maintain case you ask? well, quite easy, don’t create your structs using dot notation, explicitly set your key names by wrapping them in single quotes, check out this example:

<cfset stPerson = structNew()>
<cfset stPerson['name'] = "Sandy">
<cfset stPerson['address'] = "12 Amazing Street">
<cfset stPerson['suburb'] = "Amazingville">
<cfset stPerson['state'] = "SA">
<cfset stPerson['phone'] = "54321234">
<cfdump var="#stPerson#">

and the result:

struct
address 12 Amazing Street
name Sandy
phone 54321234
state SA
suburb Amazingville

See!! the case of the keys has been maintained!!!

If there is one thing that CF does badly it is the over-zealous creation of whitespace characters that fill your html output with unnecessary garbage that makes it near impossible to make sense of. One thing it is very good at however is making it easy to capture the output of complicated logic without the need to constantly append to a variable by using the cfsavecontent tag. The problem with this is that for every space character inside cfsavecontent it is also added to the variable being used to capture the output so something like this:

<cfsavecontent variable="htmlStr">
	<cfoutput><p>
	<cfloop query="qPets">
		<cfif qpets.cat EQ "dog">
			Some other string about dogs<br>
		<cfelse>
			Some other string about cats<br>
		</cfelse>
	</cfif>
	</cfloop></p></cfoutput>
</cfsavecontent>

Would probably produce something like this:

		<p>
							Some other string about dogs<br />

			Some other string about dogs<br />

					Some other string about cats<br />

	</p>

Luckily we have a tag that helps to repress all this whitespace inside cfsavecontent called cfprocessingdirective with the attribute suppresswhitespace set to true. It has to be used correctly, everything that you want outputted HAS to be wrapped in individual cfoutput tags eg:

<cfsavecontent variable="htmlStr">
	<cfprocessingdirective suppresswhitespace="true">
		<cfoutput><p></cfoutput>
		<cfloop query="qPets">
			<cfif qPets.cat eq "dog">
				<cfoutput>Some other string about dogs<br></cfoutput>
			<cfelse>
				<cfoutput>Some other string about cats<br></cfoutput>
			</cfif>
		</cfloop>
		<cfoutput></p></cfoutput>
	</cfprocessingdirective>
</cfsavecontent>

And the results are:

<p>Some other string about dogs<br />
Some other string about dogs<br />
Some other string about cats<br />
</p>

Easy to use, keeps the code looking snazzy and improves your html output.

I was debugging an ajax issue in IE6 today and discovered the getJson() ajax function is not resonding and there were no Javascript error. I did some googling and found the issue in the An IE AJAX gotcha: page caching article. While the article gave us a detailed description of the issue and a solution, I have a simpler suggestion which is to replace the jQuery.getJson() function with jQuery.ajax() instead. Using the ajax() function you can set the dataType to ‘json’ and that will automatically clear the caching for you. Problem fixed!

Just found this cool online tool that allows you to quickly draw up a database schema with export function that output a script for you to import the tables and all of it’s attributes into your selected database type.

http://www.dbschemaeditor.com/OnlineDB.aspx

I don’t need to write anything on the benefits of using CFQUERYPARAM, there have been many articles over the years doing just that. Instead I am going to cut to the chase and simply state -

‘If you are using CFQUERY, then you should always use CFQUERYPARAM when passing in typed data, end of story’.

OK, that was a bit short and dull, here are 3 very good reasons:

1. Reduces the risk of SQL injection attacks
2. Makes data more ‘SQL happy’, escapes problematic characters, eg: single quotes, forward slashes etc…
3. Forces you to think about the data being passed into your query, less mistakes made.

Not enough information for you? read this excellent article from Adobe – http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=queryDB_5.html#1142383

If there was anything that could be called a ‘champagne moment’ from the developers at Adobe it was when they came up with the idea of that excellent tag – CFDUMP.
I am sure when this piece of brilliance was brought to life there were many cheers and congratulatory back slapping amongst the developers not to mention the week long party and holiday bonus in the Bahamas (well maybe not that much celebration).

No other language has an easier, more elegant and quicker debugging utility that gives as much detail and information as CFDUMP. It is one of my favorite things about developing in Coldfusion, if only there was some way of getting the same sort of information in a similar manner in Javascript…. well it turns out that there is!

I was wandering around the web when I stumbled across John Bartletts Coldfusion tips n tricks. He has taken some code that essentially dumps a javascript array and given it Coldfusion like formatting. I have often wanted something just like this to save me the hours of developing ways to debug large arrays of javascript data and now here it is –

http://johnwbartlett.com/cf_tipsntricks/index.cfm?TopicID=85

Give it a shot and I am sure you will agree it is a beautiful thing.

I’ve recently started building my first html based Air application using jQuery and have came across this very bizarre image issue. So here’s the scenario, I’ve got a text link with a image background attached with an onClick function, so when a click event occurred, the image background changes to another image. Now on a normal browser that would work fine but when I ran that in an air environment, the second image just wouldn’t load. I’ve double checked the syntax and even tried using a CSS class that contains the background image and using jQuery to add the class to the link upon onClick event. As expected that didn’t work!

After hours of testing and googling, I have came to a conclusion that jQuery can’t dynamically load images in an Air environment. So what I had to do was to pre-load all div elements with all the required classes on the page for the text link and that will pre-load all the images. Therefore when an onClick event occurs, the background images are displayed. Problem fixed!

Though this is not the best solution but developers should be keep this in mind when developing your Air application, because it would seriously affect how you’ll build/structure your application and could potentially save you hours of head scratching and frustration.

I discovered this a bit by accident. I was wondering if it was possible to query 2 MYSQL databases in the one CFQUERY as I had 2 tables that I wanted to join in 2 separate databases and instead of moving the tables in question into the same database I thought I’d have a play… here are the results:

Lets make things nice and simple and call the databases database1 and database2… it’s easier. I should also mention that the 2 databases are located on the same MYSQL server and are accessed with the same user account. I have a Coldfusion datasource connected to database1 called database1connector. Here is the query and the syntax, it’s all about aliases people!

<cfquery name="q" datasource="database1connector">
	SELECT tb1.name,tb2.address
	FROM table1 tb1
	INNER JOIN database2.table2 tb2
		ON tb2.id  = tb1.id
</cfquery>

Note that I include the second database by name in the join clause, simply referring to it by ‘databasename.’ then the table name. Note that I don’t use the ‘databasename.’ notation for the database being accessed via the CF datasource (the first database, ie database1 in the above example) as it will error.

I haven’t tried it but I am guessing that it would be possible to essentially join multiple databases using this same setup, give it a go!

Ever have a lower case string and needed the initial letter of each word to be in capital? well I did and luckily found a nice regular expression via google.

rereplace(“this string is all lowercase”, “(\b\w)”, “\u\1″, “all”) would produce ‘This String Is All Lowercase’

problem solved.

The other day while I was walking to work I found myself day dreaming. As my thoughts wondered I dreamt of a perfect world where you could use regular expressions whenever, wherever you liked.

Once arriving at work I was back to reality not everything in life can be a regular expression!

As I worked away writing some boring queries, I figured I might liven up my day and try using regular expressions to improve a query. After some research, much to my surprise I found that MySQL could handle regular expressions… maybe my day dream was coming true.

In my case I was looping over a list and comparing with another list found within the database, previously I had been using numerous ‘LIKE’ statements to match the two lists (see below)

<cfquery name="qData" datasource="#application.dsn#">
 SELECT *
 FROM tableName
 WHERE status != 'Archive'
 AND
 <cfset iCount = 1 />
 <cfloop list="#arguments.groups#" index="i">
 <cfoutput>
 (availability LIKE '#i#,%' OR availability LIKE '%,#i#' OR availability LIKE '%,#i#,%' OR availability = #i#)
 </cfoutput>
 <cfif iCount LT listLen(arguments.groups)>
 <cfoutput> OR </cfoutput>
 </cfif>
 <cfset iCount = iCount + 1 />
 </cfloop> 

 ORDER BY #arguments.sort# #arguments.sortOrder#

 </cfquery>

After some messing about with regexp (and some help from my boss) I was able to reduce this to a simple regular expression so my query became.


<cfquery name="qData" datasource="#application.dsn#">
 SELECT *
 FROM tableName
  WHERE status != 'Archive'
 AND
 <cfset iCount = 1 />
 <cfloop list="#arguments.groups#" index="i">
 <cfoutput>
 availability REGEXP '[^0-9]*(#i#)[^0-9]*'
 </cfoutput>
 <cfif iCount LT listLen(arguments.groups)>
 <cfoutput> OR </cfoutput>
 </cfif>
 <cfset iCount = iCount + 1 />
 </cfloop> 

 ORDER BY #arguments.sort# #arguments.sortOrder#

 </cfquery>

Next time you are writing a query take a second to ask yourself is this a good time to try out regexp? and hopefully the answer will be yes.