Finally back up…

Well, I switched hosts a while back and sorta stopped posting anything on my blog. I don’t have everything back running yet, but am going to get things fixed completely this week. :-)

RIA Forge

If you aren’t familiar with RIA Forge, it is the main site for all things that are open source and related to adobe products (like Coldfusion). I often forget about this site and all the really neat stuff there is there so I figured it was worth posting:

http://www.riaforge.org/

Parsing the FIELDNAMES Variable for Dynamic Form Processing

So something I’ve done for a number of years now is to parse the FIELDNAMES variable from the FORM scope to process a form submission.  I thought I’d blog about it after a co-worker heard me talking about it and didn’t really know what it was. The FIELDNAMES variable is a built-in variable available in Coldfusion on any form’s ACTION page.  It holds a comma delimited list of all the form fields that were submitted from the form.

Parsing the FIELDNAMES variable can be a good way to process form data dynamically.  It is especially useful for very long forms or forms that change frequently and keeps you from having to update your action page.  In the following example, we take a standard form and submit to an action page that inserts our form data into a database:

We need our database with a defined table, a form and an action template.  My sample table in MySQL looks like this:

DROP TABLE IF EXISTS `bradtest`.`users`;
CREATE TABLE  `bradtest`.`users` (
`id` int(10) unsigned NOT NULL auto_increment,
`fname` varchar(45) NOT NULL,
`lname` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Remember the column names, as we will use those for our form field names.  This is because for this to work right, you must use your database column names as your form field names.  So create yourself a form, or use mine below.  It doesn’t matter how many form fields you have as long as you have a matching column in your table:

<cfform action=”form_process.cfm” method=”post”>
<table>
<tr>
<td>First Name:</td><td><cfinput type=”text” name=”fname” ></td>
</tr>
<tr>
<td>Last Name:</td><td><cfinput type=”text” name=”lname” ></td>
</tr>
<tr>
<td>Email:</td><td><cfinput type=”text” name=”email” ></td>
</tr>
<tr>
<td>Password:</td><td><cfinput type=”password” name=”password” ></td>
</tr>
<tr><td colspan=”2″><input type=”submit” value=”Submit”></td></tr>
</table>
</cfform>

For the last step, just need only parse our results on the action page.  The first step is only needed if you are going to insert into a database.  I’m going to calculate the number of commas I need seperate items in my SQL statement:

<cfset commas = #ListLen(form.fieldnames)#>

We actually need one less comma than this value, and it doesn’t come into play until later.  Next I’m going to duplicate some code just for the purpose of displaying our results.  We will use this CFLOOP routine again in our CFQUERY tag:

<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
<cfoutput>
#ListGetAt(form.fieldnames,fieldCount,”,”)# =
#Evaluate(ListGetAt(form.fieldnames,fieldCount,”,”))#
<br>
</cfoutput>
</cfloop>

Notice we are just displaying the raw value of the fieldname element and then we must EVALUATE the same thing to pull the value from the form field posted to our action page.  In fact, you can run the template at this point and you should see a list of the form fields, and equal symbol followed by the value submitted in said form field.

To insert the record, we just need to loop our results inside a CFQUERY tag like so:

<cfquery name=”qInsert” datasource=”MyDSN”>
insert into users
(<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
#ListGetAt(form.fieldnames,fieldCount,”,”)#<cfif fieldCount lt #commas#>,</cfif>
</cfloop>)
values
(<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
‘#Evaluate(ListGetAt(form.fieldnames,fieldCount,”,”))#’<cfif fieldCount lt #commas#>,</cfif>
</cfloop>)
</cfquery>

Notice I’m running the loop twice, once for my list of column names (remember we made sure that our table has the same column names as we used for our form field names) and then once for the corresponding values.  Also key here is the CFIF blocks in each loop to determine if we need a comma.  I know I need one less comma than I have form fields so I place one as long as our loop count is less than the number of elements in the FIELDNAMES array.

That’s it, you now have an action form that can process any number of form fields and any changes you make will automatically be accomodated for.  Just remember any changes to your database table must be reflected in your form.  Here is our working sample:

http://brad.melendy.com/projects/coldfusion/fieldnames/form_data.cfm

Get the MAC Address with Java

I recently needed to get the MAC address from staff workstations as a means of uniquely identifying which remove office the PC resided in.  Network IP was not reliable because multiple offices share the same router.  Cookies weren’t good enough because systems get wiped each night in an automated process to maintain a static workstation image.  That left either reconfiguring the networks at multiple locations, or to find a unique identifyer on each system that persists.  Here’s to the MAC address which is unique to each network adapter. 

So, to do this qould require I use some kind of deployable application that can access the local system.  As I searched around, it appeared Java was a good option but I haven’t used Java outside school.  Luckily, I stumbed across Tim Desjardins’ blog entry on the topic.  He had written a java applet to do this very task. 

I ran into one oddity with Windows Vista.  It will produce multiple MAC addresses (for loopback adapters and such) and some will actually contain only spaces.  I used Javascript to clean up the results and got a working demo here:

http://brad.melendy.com/projects/java/macAddress.cfm

You will be prompted to allow the applet to run on your system through a browser security warning.  Once you give consent, the button will display one or more of your MAC addresses in a Javascript alert.  I am capturing this information into a database as a unique identifier for each workstation.  I actually store it in a cookie for the duration of the day, but each morning I collect it anew as staff/visitors enter a particular system.

Great Object Oriented Tutorial

I’d been looking all over for a good Coldfusion Object Oriented tutorial and I finally found it.  Roy Martin has a really exceptional video tutorial online here.  What makes his unique is that it has been developed specifically for Coldfusion programmers who have little or no object oriented experience.

He does this by taking a small CRUD application written in an “old school” style (non-object oriented) and he converts it before your eyes to an object oriented version.  The source files are available for download and you can actually work along side the video as it progresses, pausing as needed to execute your code changes.  I found I understood what was going on so much more because I could see it translated to how I have coded CF for so many years.

I didn’t have to try and wrap my brain around a bunch of abstract ideas concerning apples and oranges, I could instead see my database queries and screens transform from inline spaghetti code to a modern MVC/OOP implimentation of Coldfusion.  Thanks Roy Martin!  :-)

Object Oriented Debate Again…

So the Object Oriented Debate came up at work today… yet again.  I’m a member of a six person team and only a couple of us have any experience with OOP, and then even less with regard to CFOOP (Coldfusion Object Oriented Programming).  The old guard continue to be opposed to OOP breaking into our processes even though it has surely been the trend for Coldfusion since the release of MX. 

I personally resisted OOP at first as I wondered if it really had a place in our section.  We seemed to get by fine with our homegrown “frameworks” (if you can call it that), shared library of includes and user defined functions (written in cfscript) and good old fashioned spaghetti code.  ;-)   However, over time I have felt compelled to take the plunge.  So I’m working on applying OOP and Model-Glue to my next project. 

Model-Glue is an object oriented Coldfusion framework that pretty much requires you to practice OOP in an MVC architecture.  I’ve been using Fusebox for my last couple projects, which does have an OOP implimentation, however, I find Model-Glue a more “native” OOP framework.  Fusebox 5 will support both approaches but I never could wrap my head around the OOP implimentation, possibly because I was use to the old 4.x style. 

Anyway, I’ve decided to take the plunge and that means “sneaking” it into projects here at work.  I’m finding that it isn’t something that yeilds quick returns, but in the long run, code is indeed more managable.  But to get to the point of reaping a return on your investment, you must get beyond the initial learning curve, and also any barriers imposed by your team.  It is really a question of “change management”.  People often don’t like change especially if the benefits aren’t clear or immediate as is the situation with adoption of OOP.

Next Page »