Coldfusion Interactive Code

So my previous post discussed points about how sometimes being a web programmer can be monotonous.  To avoid some of the tedium, the best thing we can do is create shortcuts for ourselves by having reusable code.  This could be in the form of functions or modules, and sometimes (though rarely in a custom programming shop) entire applications.  My favorite thing to do for setting up reusable code has to do with drop down functionality.  In most cases drop downs aren’t going to change every minute, but they will change occasionally.  In the larger projects we do, we will set up administrative functions that will provide our clients the ability to make changes to these at any time.  Of course with selection lists, if the data goes missing completely it can make previously entered pieces of data go awry, but that’s another problem entirely.

In Coldfusion, I use the cffunction tag within a cfc to store all of my regularly used drop down selection items.  At this time, if I know the selection lists don’t change every day, I will create an application scoped cfc object to hold my database queries and then functions to display the drop downs.  There is also then an administrative function to update the selection list and reset the values across the entirety of the application.  It makes things easy to update and easy to reuse.  ‘Recycle and Reuse’ shouldn’t just be a theme for environmentalists, it should also be a theme for interactive web programmers.

So how is this done?  Well, to make it simple, I’ve created a cfc that I load into the application scope via ‘createObject’.  This sets up a nice application object that I can call functions from at any time.  On initialization, it sets internal variables using the ‘this’ keyword.  I do this by creation of a function which uses cfquery to call up the data from the database.  Lets say for example my function is this: setStatesQuery();.  SetStatesQuery will get a query results table and set it to this.qStates which then can be used over and over again direct from memory, never having to hit the database again.  When the admin makes a change to the database, we just then rerun setStatesQuery() to get the updated information into the function.  Then I create a function to return the query that’s stored in memory getStates() as well as the function that creates the drop down:

<cffunction name=”createStateDropdown” returntype=”string” output=”false”>
<cfargument name=”StateAbbrev” default=”" type=”string”>
<cfargument name=”fieldIDName” default=”StateID” type=”string”>
<cfargument name=”className” default=”text” type=”string”>
<cfargument name=”oneBlank” default=”false” type=”boolean”>

  <!—get the states query—>
<cfset var qStates = getStates()>
<cfset var returnString = “”>
<!—State Abbreviation is the state id in this function—>
<cfsavecontent variable=”returnString”>
<cfoutput>
<select id=”#Arguments.fieldIDName#” name=”#Arguments.fieldIDName#” class=”#arguments.className#”>
<cfif Arguments.oneBlank><option value=”"> </option></cfif>
<cfloop query=”qStates”>
<option value=”#qStates.StateAbbrev#” <cfif Arguments.StateAbbrev eq qStates.StateAbbrev>selected=”selected”</cfif>>#qStates.StateName#</option>
</cfloop>
</select>
</cfoutput>
</cfsavecontent>
<cfreturn returnString>
</cffunction>