TAGGED AS: coldfusion

Sunday March 15, 2009 at 13:40

ColdFusion - XmlAttributes and StructKey names

I’ve been playing around with the Tumblr API for a while, reading in tags and other things.  I wanted to start storing my content, for later use, or in case anything ever happened to Tumblr.  The last thing I need is to wake up one morning and discover a giant ad running on my site.  For some time now, I have been reading and parsing the JSON version of the API. But, if I really wanted to interact with my ColdFusion server and store data, I needed to start taking a more in-depth look at the standard XML API.  The first thing I noticed, is that there are lots of XML elements that have dashes.  I’m not a fan of using dashes for anything, especially not XML element names.  In a past life, I did use underscores a great deal, but I now have moved entirely to camel case conventions.  

In any case, dashes have always caused problems for me, especially when parsing XML with ColdFusion.  If we read in the API like this:

<cfset myDoc=XMLParse("http://tumblr.themadness.org/api/read?start=#startAt#&num=50")> 

We’ll get back a piece of data like this:



You’ll notice that post->XmlAttributes contains an element called “unix-timestamp”. I prefer to use unixtime whenever possible. It’s more portable and easier to work with.

Normally, I would do something such as this to loop over all the records:
<cfset numRecs = arrayLen(myDoc.tumblr.posts.post)>
<cfloop from="1" to="#numRecs#" index="i">
#mydoc.tumblr.posts.post[i].XmlAttributes.type#
<cfloop>

However, if you reference unix-timestamp this way, it will generate an error. Instead, it needs to be referenced as:
#mydoc.tumblr.posts.post[i].XmlAttributes['unix-timestamp']#

Like everything else in ColdFusion, XML data is nothing more than a structure, and can always be referenced the same way as other structures. Once you more away from thinking of ColdFusion as just a tag system, and think of it more as a scripting language based around structures, you can accomplish almost anything with it.

Monday March 02, 2009 at 12:20

ColdFusion Doc Type for JSON

I’ve been writing lots of JSON calls with ColdFusion of late, and it never really occurred to me that I needed to set the Doc Type with CFCONTENT.  I noticed in the Safari Web Inspector that I was getting back errors on my JSON calls.  The errors were not fatal, but the browser was having to do more work than it needed to.  All the JSON I created in ColdFusion had a Doc Type of text/html.  The jQuery parser was expecting the Doc Type to be application/x-javascript.  I rarely use CFCONTENT, usually only for creating dynamic Excel files.  However, this seemed like the perfect time to do so.

After adding the following to the top of the CFM page:

<cfcontent type="application/x-javascript; charset=utf-8"> 
<cfsetting showdebugoutput="no"> 

  I was suddenly returning valid javascript JSON information to the browser.

And you thought CFCONTENT was one of those tags you’d never use.

Friday June 27, 2008 at 1:48

CFImage tag

I’ve always used ImageMagick in the past to resize images in ColdFusion, through cfexecute.  However, ColdFusion 8 now includes its own image manipulation tag, CFImage.  This tag is a lot easier to use, and it gives more control on a per file basis then the cfexecute with ImageMagick did.  The only thing that tripped me up the first time I used it was something I took for granted in ImageMagick.  If I wanted to resize an image based only on width, I would pass in the parameters:

convert -resize 23%x -quality 85

This would resize the image to 23% of the width, and by default, keep the ratio on the height.

At first glance, it looks like you cannot do this with the CFImage tag.  If you do not supply a height or width, the server returns an error, saying that it is required.  The solution to this is so obvious, which is why it’s probably not in the online docs.  Just leave whichever one you want to scale blank.  For example:

<cfimage source=”filename.jpg” action=”resize” width=”250” height=”” destination=”filename_th.jpg”/>

Friday June 27, 2008 at 1:29

I guess I should read the documentation for ColdFusion 8 more often.  Turns out, there is a new feature when doing database inserts.  In the CFQUERY tag, add the parameter result=&#8221;somename&#8221;.  You can now reference the data inserted into the query, as well as, a really cool new feature, the last inserted key.  Each database type has a different name for this.  MySQL calls it GENERATED_KEY.  You no longer need to do a second query to get back the last_insert_id().
 
&lt;cfquery datasource=&#8221;#application.dsn#&#8221; result=&#8221;insertPhoto&#8221;&gt;
INSERT INTO kidPhotos
(subject, body, dateOf, siteType)
VALUES
(&lt;cfqueryparam value=&#8221;#subject#&#8221; cfsqltype=&#8221;cf_sql_varchar&#8221;&gt;,
&lt;cfqueryparam value=&#8221;#body#&#8221; cfsqltype=&#8221;cf_sql_varchar&#8221;&gt;,
&#8216;#dateformat(date, &#8220;yyyy-mm-dd&#8221;)#&#8217;,
&#8216;ap&#8217;)
&lt;/cfquery&gt;

I guess I should read the documentation for ColdFusion 8 more often.  Turns out, there is a new feature when doing database inserts.  In the CFQUERY tag, add the parameter result=”somename”.  You can now reference the data inserted into the query, as well as, a really cool new feature, the last inserted key.  Each database type has a different name for this.  MySQL calls it GENERATED_KEY.  You no longer need to do a second query to get back the last_insert_id().

<cfquery datasource=”#application.dsn#” result=”insertPhoto”>

INSERT INTO kidPhotos

(subject, body, dateOf, siteType)

VALUES

(<cfqueryparam value=”#subject#” cfsqltype=”cf_sql_varchar”>,

<cfqueryparam value=”#body#” cfsqltype=”cf_sql_varchar”>,

‘#dateformat(date, “yyyy-mm-dd”)#’,

‘ap’)

</cfquery>

Page 1 of 2