Skip to content

Instantly share code, notes, and snippets.

@sipacate
Created September 17, 2012 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sipacate/3739550 to your computer and use it in GitHub Desktop.
Save sipacate/3739550 to your computer and use it in GitHub Desktop.
Basics

In our last Chapter, we talked about variables, data and how to transport data around in your application. There are different kinds of data and each helps in specific ways.

Strings/Numbers

Is Simple: Yes

Strings and numbers are very easy to work with. To set a string or a number, just use the CFSET command. You can append strings and numbers to each other using the & operator. See examples:

<cfset aString = "hi">
<cfset aNumber = 42>
<cfset aStringAndANumber= aString & aNumber>

aString: <cfoutput>#aString#</cfoutput>
aNumber: <cfoutput>#aNumber#</cfoutput>
aStringAndANumber: <cfoutput>#aStringAndANumber#</cfoutput>

If you have a big block of strings to set, you can use the CFSAVECONTENT command.

<cfsavecontent variable="EmailContent">
	Hi 

	We want to send you a hoverboard.
	Let us know if you will accept this free offer.

	-Us
</cfsavecontent>

<cfoutput>#EmailContent#</cfoutput>

Dates

Is Simple: Kind of

Dates are also very easy to work with in ColdFusion. You can use built in functions like now() to make a date, or you can just type the date into the variable assignment like this:

<cfset DateToday = now()>
<cfset NewYearDay = "1/1/2013">

You can use built-in functions to work with dates. Say you wanted to know how many days it has been since the turn of the century:

<cfset DaysSinceTurnOfCentury = DateDiff("d", "1/1/2000", now() )>
<cfoutput>#DaysSinceTurnOfCentury#</cfoutput>

Or suppose you want to know what the date will be 42 days from now:

<cfset FourtyTwoDaysFromNow = DateAdd("d", now(), 42 )>

ColdFusion provides a lot of useful functions to work with dates and times. You can compare dates, find out how far two dates are apart, reformat a date, and lots of other useful actions. See the ColdFusion Date/Time documentation for a full listing: http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-6986

Arrays

Is Simple: No

Arrays are an ordered series of data. Here's an example of a one dimensional array:

Array Creation

<cfset ThingsILike = ["Warm Sandy Beaches", "Tropical Drinks", 42]>
<cfdump var="#ThingsILike#"> 

Alternate Method: Array Creation

Here's another way to create an array, along with a couple of different ways to add data to an array:

<cfset ThingsILike = arrayNew(1)>

Adding items to an Array

You can add things by a specific position. Note: Arrays in ColdFusion start at 1, not 0.

<cfset ThingsILike[1]  = "Warm Sandy Beaches">

Alternate Method: Adding items to an Array

You can append an item to the end of the array

<cfset ArrayAppend( ThingsILike,  "Tropical Drinks")>
<cfset ArrayAppend( ThingsILike,  42)>
<cfdump var="#ThingsILike#"> 

See how I defined the strings in my array with quotes, and non-strings without? Each element in the array is an execution zone also, so if you need ColdFusion to evaluate something, just add it in:

<cfset ImportantDates = ["12/26/1975", now() ]>
<cfdump var="#ImportantDates#">

Displaying the Contents of an Array

You can not use the CFOUTPUT command on an array. This is because complex data types like arrays are not displayable by a string. You can loop over the array, however, and output the strings to the page like this:

<cfset ThingsILike = ["Warm Sandy Beaches", "Tropical Drinks", 42]>
<cfloop array="ThingsILike" index="thing">
	<cfoutput>#thing#</cfoutput>
</cfloop>

ColdFusion provides a lot of useful functions to work with arrays. You can search the contents of an array for a value, perform mathematical functions like Sum or Average, sort the contents of an array and other useful actions. See the ColdFusion Array function documentation for a full listing:
http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-6a66

Structs

Is Simple: No

Structs are a collection of data, stored by a key, or name. Suppose for example, you wanted to store several kinds of fruit and also whether you like it or not. Structs provide a way to organize like name/value pairs and let you refer to them as a single collection.

Struct Creation

<cfset FruitBasket = structNew()>

Alternate Method: Struct Creation

<cfset FruitBasket = {}>

Adding items to a Struct: Bracket Notation

<cfset FruitBasket = {} />
<cfset FruitBasket["Apple"] = "Like">
<cfset FruitBasket["Banana"] = "Like">
<cfset FruitBasket["Cherry"] = "Dislike">

<cfdump var="#FruitBasket#">

Adding items to a Struct Dot Notation

<cfset FruitBasket = {} />
<cfset FruitBasket.Apple = "Like">
<cfset FruitBasket.Banana = "Like">
<cfset FruitBasket.Cherry = "Dislike">

<cfdump var="#FruitBasket#">

Pro Tip: There are reasons to use one struct notation over another. Did you notice the Bracket Notation preserved the case of the keys and the Dot Notation did not? Sometimes the preservation of case is important, like when passing values to Javascript or other case sensitive languages or formats. Also, the bracket notation allows for a dynamic key reference. This is helpful when the name of the struct key will come from a runtime operation, like looping over the struct. Many people find Dot Notation easier to read and use it most of the time, except in cases where Bracket Notation offers a feature Dot Notation does not.

Displaying the Contents of a Struct

See how your preference was mapped to the kind of fruit? Now, you can't use the CFOUTPUT command on structs either, because once again, they just aren't displayable by a string. You can loop over the struct and output the keys and values to the page, if you want:

<cfloop collection="#FruitBasket#" item="fruit">
	<cfoutput>I #FruitBasket[fruit]# #fruit#</cfoutput><br />
</cfloop>

ColdFusion provides a lot of useful functions to work with structs. You can search the contents of struct, make a complete copy, retrieve a list of just the keys and many other useful actions. See the ColdFusion Struct function documentation for a full listing:
http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-69b8

Queries

Is Simple: No

Queries are recordsets. Recordsets contain a series of columns with 0 or more rows. You can think of a query like a single page of a spreadsheet with the columns across the top and rows down the side.

Most ColdFusion programs interact with databases. Database interaction takes the form of a query and ColdFusion makes it very easy to work with the data returned by the database.

<cfquery name="FruitQuery" datasource="fruit">
	SELECT Name, Price
	FROM FruitStore
	WHERE Price < 7
</cfquery>

<cfloop query="FruitQuery">
	#FruitQuery.Name# costs #FruitQuery.Price# <br />
</cfloop>

Query Objects have a few special properties. You can use these properties to get specific information about the data inside the query.

  • Queryname.recordcount = How many rows does this query have?
  • Queryname.columnlist = What columns does this query have?
  • Queryname.currentrow = What row number are we currently on?

ColdFusion provides several useful functions to work with query objects. You can create your own query objects in ColdFusion without a database call or even retrieve all the values in a specific column in a list format. See the ColdFusion Query function documentation for a full listing:   http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-67fe

Also, be sure to review the Accessing and Using Data portion of the ColdFusion documentation explains a number of key concepts and techniques for working with data, databases and query objects: http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WS8f0cc78011fffa7168855f811cdb0b0cce-8000.html

@atuttle
Copy link

atuttle commented Sep 18, 2012

Formatting for your CFSaveContent sample code is wrong and the tags are hidden. Not sure why you quoted it (by putting > at the beginning of each line) but removing that should fix it.

Dates: Maybe link to the documentation's list of date functions? Or at least mention that there are a lot.

Structs: You don't cover creating keys via dot-notation at all: <cfset FruitBasket.Mango = "smother all over my body" />. I also think it would be worthwhile to show the output of your loop to display the contents of the struct. I Dislike Cherry<br /> etc...

Queries have a few special properties.

I might rephrase as "Query objects have..." or "Query results have..."

Also your list at the end... you can make this a bulleted list with:

* **Queryname.recordcout** = How many...
* ** Queryname.columnlist** = ...

Or if you don't want a bulleted list, just put <br /> at the end of each line and they'll appear similarly to how you've written them currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment