Skip to content

Instantly share code, notes, and snippets.

@atuttle
Forked from sipacate/DataTypes.md
Created September 18, 2012 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuttle/3743124 to your computer and use it in GitHub Desktop.
Save atuttle/3743124 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 like this:

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

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

Hi
  We want to send you a hoverboard.
  Let us know if you will accept this free offer.
  -Us
#EmailContent#

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 )>

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>

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

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

<cfdump var="#FruitBasket#">

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>

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>

Queries 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?

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