Archive for the ‘ColdFusion’ Category

If you’ve ever had the need to convert a query column to an array, there are a couple ways to do it. There is a queryColumnToArray function on cflib, or you could roll your own, but you can also do it with the built in ValueList and ListToArray functions.

Given the query “myQuery”, and a column called “name”, you can turn that into an array with this:

<cfset myArray = ListToArray(ValueList(myQuery.name)) />

The only problem with this is if the name column were to ever contain a comma, that value will get split into separate values, probably causing problems later on in your code. I ran into this issue with some old code the other day. The quick solution was to use a different delimiter than the default, which is a comma. You can pick a bizarre ascii character that you are unlikely to encounter in your data. I usually use one of the unprintable characters like 31, a “unit separator”, which sounds like a good one, doesn’t it?

<cfset myArray = ListToArray(ValueList(myQuery.name, Chr(31) ), Chr(31) ) />

In our application we create some large queries using QueryNew(). Before we started specifying the datatypes we would run into strange bugs occasionally. When you don’t specify the data type ColdFusion makes a guess. So if we had some code like this:

<cfset myQuery = queryNew('name,age,address,code') />

Then populate that query with data from another system (in our case, its often SAP) everything usually works fine. But ColdFusion is guessing the datatype based on the first 50 records, which can sometimes cause problems.

If for example, the first 50 records contain a ‘code’ value that is all integers, ColdFusion will give that column a datatype of integer. Then if the 51st record contains a varchar value, ColdFusion will blow up.

The solution is to declare the data types when creating the query, like this:

<cfset myQuery = queryNew('name,age,address,code' , 'varchar,integer,varchar,varchar') />

This can get a little harder to manage when the query has many columns in it. We have some with over 40 columns and it’s hard to keep track of which datatype is for which field.

I wrote this little helper function to make it easier:
Continue reading ‘Little UDF for creating typed queries: TypedQueryNew()’ »

The CFWindow tag can be useful to prompt for information on a form. We use this sometimes after a user has clicked the submit button. We do some client side validation in JavaScript, and if certain conditions are met, we use CFWindow to prompt for some additional information.

Its a little tricky though. If you try to use a CFWindow tag inside a CFForm tag you will get a ColdFusion error. If you use it inside a regular form tag you won’t get an error, but it won’t work like you expect. Lets take this code for example:
Continue reading ‘Using CFWindow within a form’ »

Capitalizing the first letter of each word – a common need for sure. There is a function for this on cflib but it didn’t work for me. No errors but it wasn’t returning what I expected. I came across this function that works, and does so with much less code:

<cffunction name="CapFirst" access="public" output="false" returntype="String">
        <cfargument name="inputString" required="false" type="String" default="" />
        <cfreturn rereplace(lcase(arguments.inputString), "(\b\w)", "\u\1", "all") />
</cffunction>

I found this snippet on the WeCodeThings blog. Good stuff.

One of the problems I looked into this week was why some items on our site that used to be in bold were no longer displaying as such. Viewing the source showed me that the text was indeed surrounded by <strong> tags.

Using the awesome Firebug extension for Firefox, I could see that the ext-all.css file was disabling the font-weight on the strong tag. This was quite surprising to me!

The ext-all.css file will automatically be included on your page whenever you use almost any of the UI features such as cfgrid, cflayout, etc.

You can easily reproduce the problem with this minimal code:

<cflayout type="tab"></cflayout>
<strong>This should be bold but its not.</strong>

You can fix the problem by adding this to your site’s style sheet, if you have one:

strong { font-weight: bold; }

If you don’t use a separate style sheet you can just put it directly into your page like this:

<style type="text/css">
strong { font-weight: bold; }
</style>

Hopefully this can be fixed before 9.01 comes out. I think its a pretty big deal, it disabled tons of bold content on our site. A lot of our content is created using the FCK rich text editor, which uses strong tags to indicate bold. I’ve file a bug report with Adobe, you can vote for this bug here: http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=82156

After looking through the EXT documentation, I think this css originates with the EXT group. But when Adobe repackages EXT into CF, they need to either override this style it or take it out of the css file completely.

A few weeks ago, the people at CFWheels announced a contest to get people to try out CFWheels. To enter the contest all you need to do is build a version of litepost in CFWheels. If you haven’t heard of litepost, its a simple blogging app thats been used to demonstrate different ColdFusion frameworks. Really all you need from the litepost project is the database. Then just build some CRUD for the users, entries, comments, etc. I’ve been wanting to learn more about this framework for a while so I thought this contest would be a good excuse to check it out. The top 3 winners get Amazon gift cards.

So far I’ve found this to be a pretty neat framework. I see a lot of similarity to Rails. The documentation is very good. The plugins are pretty neat, you just drop a zip file into your plugins directory and you can start using that plugin.

To start I downloaded cfwheels and setup my database. I installed the scaffolding plugin and used that to generate my CRUD views, models and controllers. Already I had the basics working! I tried creating/listing/editing users and it worked great.
Continue reading ‘Trying out the CFWheels framework’ »

A while back I wrote a post explaining how to wrap the tabs generated by the new cflayout tab in ColdFusion 8.

The tabs generated by ColdFusion 9 are completely different, since they use Ext version 3. ColdFusion 8 used Ext 1.

The tabs in ColdFusion 9 do not wrap by default, but they do automatically add scroll controls so you can scroll through the long horizontal line of tabs. If you’d rather have them wrap into multiple lines, this little bit of CSS will do it:

.x-tab-panel ul.x-tab-strip {
    width: auto !important;
}

I just found out I was accepted into the Adobe Community Professionals program for 2010. I hope I can be a good resource for the ColdFusion community. I look forward to continuing to blog and discuss ColdFusion. The CfObjective conference in Minneapolis is just a few months away. I’ll be there, come say hello if you see me.

Here is the announcement http://lizfrederick.blogspot.com/2010/01/new-acps-for-2010.html

One of the things I’ve noticed as I’ve been working on our ColdFusion 9 upgrade is that many of the UI components look different than they did in ColdFusion 8. In many cases I’m sure this won’t be a problem. But my user base is fairly picky, and I think the CF8 to CF9 transition should be seamless to them. The tabs in CF9 look quite a bit different than they do in CF8.
Continue reading ‘Making ColdFusion 9 tabs look similar to CF8 tabs’ »

A friend asked me for some help the other day. At the place he works at they use IIS on their production servers, but each developer works locally using Apache. Why use different webservers for production and development? He said they do it because its easier to develop locally with Apache. My guess is its easier because you can run multiple sites at once with Apache. With IIS on a desktop machine you can only run one site at a time. I do the exact same thing at my job, actually.

Anyway, once in a while this causes issues. In this case, there was code on the server that required a username and password in order to access the page. The security was done at the web server level, aka “HTTP authentication”, which causes your browser to prompt you for a username and password. These credentials then get sent along in the request headers (its actually a little more complicated than that but I won’t get into that here). After authenticating, the username is available to ColdFusion as a CGI variable – CGI.REMOTE_USER.

When using IIS, that value is also available as CGI.AUTH_USER. In all CGI variable specs I could find, they all reference REMOTE_USER, not AUTH_USER, I’m not sure when AUTH_USER started to be used. Anyway, this ColdFusion code running on the IIS server would look to the CGI.AUTH_USER variable and display some things differently depending on who the user was.

This posed a problem when trying to run this code locally under apache. The CGI.AUTH_USER variable did exist, but it was always blank. One could change the code to use the more multi-platform friendly “REMOTE_USER”, but sometimes there are hurdles to changing existing code.

But there is a way to mimic the behavior of IIS, by copying the REMOTE_USER value into AUTH_USER. Its only three simple lines but it took me quite a while to figure this out.

RewriteEngine on
RewriteCond %{REMOTE_USER} (.*)
RewriteRule .* - [E=AUTH_USER:%1]

You’ll need to have mod_rewrite enabled of course. Usually all you need to do is uncomment a line that looks like this in httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

The three magic lines can go into the httpd.conf file, or you could place them in a .htaccess file in the directory you’re working in.