Cold Fusion in 100 Minutes

Published on December 2016 | Categories: Documents | Downloads: 30 | Comments: 0 | Views: 381
of 10
Download PDF   Embed   Report

Comments

Content

Signup and Pricing

Explore GitHub

Features

Blog

Login

mhenke / CFML-in-100-minutes
Code Network Pull Requests
0

Watch

Fork

42

26

Issues

14

Stats & Graphs

branch: master

Files

Commits

Branches

3

Tags

Downloads

Latest commit to the master branch

Merge pull request #34 from designfrontier/develop
mhenke authored July 12, 2011
commit 52ae81709f

CFML-in-100-minutes / cfml100mins.markdown
100644 953 lines (703 sloc) 35.224 kb
raw

Edit this file

blame

history

CFML in 100 minutes
ColdFusion Markup Language (CFML) is a web programming language, which is especially suited for new developers as it was written to make a programmer's job easy and not care if the computer's job is hard. In this brief introduction we'll look at key language features you need to get started. 1. 2. 3. 4. 5. 6. 7. 8. Syntax Variables Components, Methods, and Parameters Strings Numbers Queries Arrays Structures

9. Conditionals 1. If, Else If, & Else 2. Looping 10. Nothingness & Null

CFML History
Although many people think of ColdFusion, or CFML, as an old programming language, Allaire, Macromedia, and Adobe have all devoted resources to its continued improvement. ColdFusion originated as proprietary technology, however, the availability of competing open source products like Railo and OpenBD has made CFML more widely available. In 1995 Jeremy and JJ Allaire invented ColdFusion to make it easier to connect simple HTML pages to a database. ColdFusion now includes advanced features for enterprise integration and application development. When ColdFusion was originally released, it was adopted in both the government and private sector. CFML tag syntax resembles HTML and the CFML script syntax, CFScript, resembles ECMAScript (JavaScript). You may want to focus on either the tag or script based examples depending on your comfort level. And you want to learn CFML so here goes!

1. Syntax
There are two ways to write CFML code. You can use tag or script syntax. For the examples, please focus on one or the other so this tutorial is not confusing. CFML includes a set of instructions you use in pages. You will write one or more instructions in a file then run the file through a CFML engine. Three CFML instructions we will use in this tutorial are CFSET, CFOUTPUT, and CFDUMP. CFSET is used to create a variable and assign it a value. Also CFSET is used to call methods. CFOUTPUT displays a variable's value. CFDUMP is used to display the contents of simple and complex variables, objects, components, user-defined functions, and other elements. We might have a file named myprogram.cfm and Sample.cfc like this:

Tag Syntax
myprogram.cfm
< c f s e t s = New S a m p l e( ) / > < c f o u t p u t ># s . h e l l o( ) #< / c f o u t p u t >

Sample.cfc

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 1 / 10

<cfcomponent> < c f f u n c t i o n name= " h e l l o "> <cfreturn "Hello, World!" / > </cffunction> </cfcomponent>

Script Syntax
myprogram.cfm
<cfscript> s = New S a m p l e( ) ; w r i t e O u t p u t( s . h e l l o( ) ) ; </cfscript>

Sample.cfc
component { public string function hello(){ return( "Hello, World!" ); } }

For the script example, myprogram.cfm would have beginning/closing < c f s c r i p t > tags around the instructions, however, the script-based Sample.cfc does not require < c f s c r i p t > tags around the instructions.

PHP Syntax
myprogram.php
<?php r e q u i r e( " S a m p l e . p h p ") ; $ s = n e w S a m p l e( ) ; e c h o $ s- > h e l l o( ) ; ?>

Sample.php
<?php c l a s s Sample { p u b l i c f u n c t i o n h e l l o( ) { r e t u r n " H e l l o , W o r l d ! "; } } ?>

Ruby Syntax
myprogram.rb
c l a s s Sample d e f h e l l o "Hello, World!" end s = S a m p l e. new p u t s s. h e l l o

2. Variables
Everything needs a name so we can refer to it. A variable, like in math, is just a name for a piece of data. In CFML, variables are very flexible and can be changed at any time. Variables are assigned using a single equals sign ( = ) where the right side of the equals sign is evaluated first, then the value is assigned to the variable named on the left side of the equals. Go into a CFML file, enter in these example instructions, and observe the output that CFML gives you back: Tag
<cfoutput> <cfset a = 5 / > a = #a#< b r / > <cfset b = 10 + 5 / > b = #b#< b r / > <cfset c = 15 + a + b / > c = #c #< b r / > <cfset b = c * a / > b = #b#< b r / > <cfset d = "Hello, " / > d = #d#< b r / > </cfoutput>

Syntax
<cfscript> a = 5; w r i t e O u t p u t( " a b = 1 0 + 5; w r i t e O u t p u t( " b c = 1 5 + a + b; w r i t e O u t p u t( " c b = c * a; w r i t e O u t p u t( " b d = " H e l l o , "; w r i t e O u t p u t( " d </cfscript> = # a #< b r / > ") ; = # b #< b r / > ") ; = # c #< b r / > ") ; = # b #< b r / > ") ; = # d #< b r / > ") ;

In this second example, we assume the first example is present. Tag
<cfoutput> <cfset e = "World!" / > e = #e#< b r / > <cfset f = d & e / > f = #f #< b r / >

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 2 / 10

<cfset g = d & a & e / > g = #g#< b r / > <cfset b = " h i ! " / > b = #b#< b r / > </cfoutput>

Syntax
<cfscript> e = " W o r l d ! "; w r i t e O u t p u t( " e f = d & e; w r i t e O u t p u t( " f g = d & a & e; w r i t e O u t p u t ( "g = b = "h i! " ; w r i t e O u t p u t ( "b = </cfscript> = # e #< b r / > ") ; = # f #" < b r/ >" ) ; # g # < b r/ >" ) ; # b # < b r/ >" ) ;

*The first few lines in the first example are simple if you've done any programming language before, but the last few get interesting when combining strings and numbers. The code looks a little messy since after each instruction we output a variable.

3. Components, Methods, and Parameters
Components (aka Objects or Classes)
In CFML, a ColdFusion component (CFC) is a file that contains data and methods. Components are the building blocks for objects in CFML. Objects know information, called "attributes", and can do actions, called "methods". In ColdFusion the c f f u n c t i o n tag is used to define methods within a tag -based CFC. In a script-based CFC, you use the f u n c t i o n keyword to define a method. For an example of an object, think about you as a human being. You have attributes like height, weight, and eye color. You have methods like walk, run, wash dishes, and daydream. Different kinds of objects have different attributes and methods. In the next sections we'll look at a few specific instructions in CFML. In CFML we define an object using the c f c o m p o n e n t instruction (c o m p o n e n t in script-based CFCs) and save the file as .cfc. Here's an example defining the object type PersonalChef.cfc: Tag
<cfcomponent> </cfcomponent>

Syntax
component { }

Methods
Inside the CFC we usually define one or more methods using the c f f u n c t i o n or f u n c t i o n instruction like this: Tag
<cfcomponent> < c f f u n c t i o n name= " m a k e T o a s t "> < c f s e t makeToast = " M a k i n g y o u r t o a s t ! " / > </cffunction> </cfcomponent>

Syntax
component { public string function makeToast(){ makeToast = "Making your toast!"; } }

Inside the c f f u n c t i o n/f u n c t i o n instruction we would put the code for how the chef should make the toast.

Classes
A "class" is an abstract idea, it defines what all objects of that type can know and do. Think of the chair you're sitting in. It’s not an abstract chair, it is an actual chair. While a "Chair" class would represent a chair in the abstract sense, we would call the actual chair an "instance" of the "Chair" class. It is a realization of the idea of the Chair. It has measurable attributes like height, color, and weight. The class Chair, on the other hand, is abstract. It is abstract in the sense that the class's attributes, such as height, color, and weight, cannot be determined ahead of time. Once we define a class, we create an instance of that class like this: Tag
< c f s e t f r a n k = New P e r s o n a l C h e f( ) / >

Syntax
frank = New PersonalChef();

We're calling the New instruction on the class P e r s o n a l C h e f and storing it into the variable named frank. Once we have the instance, we can set or get its attributes and call its methods. Methods are called by using this syntax: o b j e c t . m e t h o d _ n a m e ( ). So if you have a person named "frank" you would tell him to make toast by calling f r a n k . m a k e T o a s t ( ). The New instruction creates a new instance of the object and calls its i n i t ( ) method (if existing). Any arguments supplied to the object will be passed to the i n i t ( ) method. The i n i t ( ) method should return the object instance using < c f r e t u r n t h i s / > in order to have the same expected behavior as the C r e a t e O b j e c t instruction. If no i n i t ( ) method exists, the object will be returned normally.

Method Parameters
Sometimes methods take one or more parameters telling them how to do what they're supposed to do. For instance, I might call f r a n k . m a k e T o a s t

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 3 / 10

( " b u r n e d " ) for him to burn my toast. Or maybe he has another method where I call f r a n k . m a k e b r e a k f a s t ( " t o a s t " , " e g g s " ) for him to make both toast and eggs. Parameters can be numbers, strings, or any kind of object. When a method takes a parameter we use the c f a r g u m e n t

instruction, it'll look like this: Tag
<cfcomponent> < c f f u n c t i o n name= " m a k e T o a s t " r e t u r n T y p e= " s t r i n g "> < c f a r g u m e n t name= " c o l o r " r e q u i r e d= " y e s "> < c f s e t m a k e T o a s t = " M a k i n g y o u r t o a s t # a r g u m e n t s . c o l o r #! " / > </cffunction> </cfcomponent>

Syntax
component { public string function makeToast(required String color){ makeToast = "Making your toast #arguments.color#!"; } }

The method is requiring us to pass in a "color" telling it how to do the method "makeToast".

Return Value
In CFML, every time you call a method you won't necessarily get a value back. By default, a CFML method returns nothing. We'll talk about nothing and null" in the last section of "CFML in 100 minutes". If you called m a k e T o a s t method above like < c f s e t r e s u l t = f r a n k . m a k e T o a s t ( " b u r n e d " ) / > or s e t r e s u l t = f r a n k . m a k e T o a s t ( " b u r n e d " ) ;, and tried to output "result" you should have seen "Variable RESULT is undefined". To return data, we use c f r e t u r n to instruct the method to return a "value". Since that wasn't in the last instruction before the ending c f f u n c t i o n in your m a k e T o a s t method, you received nothing and tried to putting that into the "result" variable. For the purposes of our next section I’m going to return the chef instance itself from the method. If you wanted to picture the metaphor, imagine you are looking at your chef "frank". You say, "Frank, go make my toast", he tells you he's making the toast, goes to make it, then comes back to you to receive more instructions. He's returning himself to you. Here's how we implement it in code: Tag
<cfcomponent> < c f f u n c t i o n name= " m a k e T o a s t " r e t u r n T y p e= " c o m p o n e n t "> < c f a r g u m e n t name= " c o l o r " r e q u i r e d= " y e s "> < c f s e t t h i s . m a k e T o a s t = " M a k i n g y o u r t o a s t # a r g u m e n t s . c o l o r #! " / > <cfreturn t h i s / > </cffunction> </cfcomponent>

Syntax
component { public component function makeToast(required String color){ this.makeToast = "Making your toast #arguments.color#!"; return this; } }

4. Strings
In CFML a string is defined as a quote ( ' or " ) followed by zero or more letters, numbers, or symbols and followed by another quote ( ' or " ). Some simple strings would be hello or This sentence is a string!. Strings can be anything from "", the empty string, to really long sets of text. This whole tutorial, for instance, is stored in a string. Strings have a few important instructions that we'll use.

Len
• Call L e n on a string to get back the number of characters in the string. For instance L e n ( " H e l l o " ) would give you back 5.

Replace
• The R e p l a c e instruction replaces occurrences of substring1 in a string with substring2, in a specified scope. The search is case sensitive and the scope default is one. For instance, R e p l a c e ( " H e l l o " , " e " , " " ) would give you back hllo after replacing the first occurrence of e, or R e p l a c e ( " G o o d M o r n i n g ! " , " o " , " e " , " A l l " ) would give you Geed Merning!

RemoveChars
• Call RemoveChars to remove characters from a string. For instance, R e m o v e C h a r s ( " h e l l o b o b " , 2 , 5 ) would give you back hbob.

Mid
• The m i d instruction extracts a substring from a string. For instance, I could call M i d ( " W e l c o m e t o C F M L J u m p s t a r t " , 4 , 1 2 ) and it would give you back: come to CFML. Experiment with the following samples in a CFML file. Tag
< c f s e t t e s t e r = "Good Morning Everyone!" / > < c f o u t p u t ># l e n ( t e s t e r) #< b r >< / c f o u t p u t > < c f o u t p u t ># R e p l a c e( t e s t e r, " o ", " e ", " A l l ") #< b r >< / c f o u t p u t > < c f o u t p u t ># R e m o v e C h a r s( t e s t e r, 2 , 5 ) #< b r >< / c f o u t p u t > < c f s e t t 2 = "sample,data,from,a,CSV" / > < c f s e t t 3 = M i d( t 2, 8 , l e n( t 2) ) / > < c f o u t p u t ># t 3# < b r >< / c f o u t p u t >

Syntax
<cfscript>

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 4 / 10

t e s t e r = " G o o d M o r n i n g E v e r y o n e ! "; w r i t e O u t p u t( " # l e n ( t e s t e r ) #< b r / > ") ; w r i t e O u t p u t( R e p l a c e( t e s t e r, " o ", " e ", " A l l ") & " < b r / > ") ; w r i t e O u t p u t( R e m o v e C h a r s( t e s t e r, 2 , 5 ) & " < b r / > ") ; t 2 = " s a m p l e , d a t a , f r o m , a , C S V "; t 3 = M i d( t 2, 8 , l e n ( t 2) ) ; w r i t e O u t p u t( t 3 & " < b r / > ") ; </cfscript>

Often a string may store a list like the t2 variable in the last example. A string for storing a list isn't the best for performance and usage. Using an array for a list is so much better. We can convert a list into an array using ListToArray. We'll discuss arrays in an upcoming section. Try out these next examples in the CFML file assuming we have the code from the last example: Tag
< c f s e t t 4 = L i s t T o A r r a y( t 3) <cfoutput> # t 4[ 2 ] # </cfoutput> / >

Syntax
<cfscript> t 4 = L i s t T o A r r a y( t 3) ; w r i t e O u t p u t( t 4[ 2 ] ) ; </cfscript>

The numbers inside the "[]" brackets specify which item of the array you want pulled out. They're numbered starting with 1. So the first example pulls out the "2" array item. This "t4" array contains position "1", the beginning of the list, up to position "4", the ending of the array.

Combining Strings and Variables
It is extremely common that we want to combine the value of a variable with other strings. For instance, lets start with this example string: Happy Saturday! When we put that into the CFML file, it just spits back the same string. If we were writing a proper program we might want it to greet the user when they start the program by saying Happy then the day of the week. So we can't just put a string like Happy Saturday! or it'd be saying Saturday even on Tuesday. What we need to do is combine a variable with the string. There are two ways to do that. The first approach is called string concatenation which is basically just adding strings together: In the first line we setup a variable to hold the day of the week. Then we'll printed the string Happy combined with the value of the variable "today" and the string !. You might be thinking, "What was the point of that since we still wrote Saturday in the first line?" Ok, well, if you were writing a real program you'd use CFMLs built-in date instructions like this:
today = DayOfWeek(Now()); N o w ( ) gets the current date and time of the computer running the ColdFusion server. "DayOfWeek" returns an integer in the range 1 (Sunday) to 7

(Saturday) for the day of the week. We still don't have the day of week as string. Try this: Tag
< c f s e t t o d a y = D a y O f W e e k A s S t r i n g( D a y O f W e e k( N o w( ) ) ) / > < c f s e t message = " H a p p y " & t o d a y & " ! " / > <cfoutput> # m e s s a g e# </cfoutput>

Syntax
<cfscript> t o d a y = D a y O f W e e k A s S t r i n g( D a y O f W e e k( N o w( ) ) ) ; m e s s a g e = " H a p p y " & t o d a y & " ! "; w r i t e O u t p u t( m e s s a g e) ; </cfscript>

Great, no errors and our output looks correct. "DayOfWeekAsString" did the trick. There is another string combination called string interpolation. String interpolation is the process of sticking data into the middle of strings. We use the symbols # around the "variable" where in a string the value should be inserted. Inside those hashes we can put any variable and output it in that spot. Our previous example "message" could be rewritten like this: Tag
< c f s e t m e s s a g e = " H a p p y # t o d a y #! " / >

Syntax
message = "Happy #today#!";

If you compare the output you'll see the second example gives the exact same results. The code itself is a little more compact and, personally, I find it much easier to read. Basically interpolating means evaluate the code inside this # wrapper and put it into the string.

5. Numbers
There are two basic kinds of numbers in CFML: integers (whole numbers) and real (numbers with a decimal point). For our workshop, we'll only be dealing with integers. You can use normal math operations with integers including "+", "-", "/", and "*". The "++" operator can be used to increment a number. It is also the only one we will use to control a loop. We will talk more about Conditional Looping in section 9. Try out this example for the "++" operator: Tag
<cfset loop = 0 / > <cfoutput> < c f l o o p c o n d i t i o n= " l o o p L T 5 " >

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 5 / 10

# l o o p# H e l l o , w o r l d !< b r > < c f s e t l o o p+ + / > </cfloop> I a m h e r e !< b r > </cfoutput>

Syntax
<cfscript> f o r( l o o p = 0 ; l o o p < 5 ; l o o p+ +) W r i t e O u t p u t( " # l o o p # H e l l o , w o r l d ! < b r > ") ; W r i t e O u t p u t( " I a m h e r e < b r > ") ; </cfscript>

In this next example we're using the c f l o o p instruction with a multiple instructions inside the condition. The CFML script syntax looks for the starting { and the ending } . Each instruction between the beginning { and ending } will be executed if the condition is true. In the tag example there's no need to manage the index inside the loop if you're simply stepping through one item at a time. You can use the f r o m and t o arguments, and ColdFusion will simply loop from the first value to the second, and automatically increment the variable in the i n d e x argument. Try this example with multiple instructions: Tag
<cfset loop = 0 / > <cfoutput> < c f l o o p i n d e x= " l o o p " f r o m= " 0 " t o= " 4 "> # l o o p# G o o d M o r n i n g ! . . . i s i t l u n c h t i m e y e t ?< b r > </cfloop> </cfoutput>

Syntax
<cfscript> l o o p = 0; w h i l e( l o o p < 5 ) { W r i t e O u t p u t( " # l o o p # G o o d M o r n i n g ! ") ; W r i t e O u t p u t( " . . . i s i t l u n c h t i m e y e t ? < b r > ") ; l o o p+ +; } </cfscript>

It's also possible to go through a loop and step over more than one value at a time. The following examples will step through the loop and increase the "loop" index by two for each time through the loop. Tag
<cfset loop = 0 / > <cfoutput> < c f l o o p i n d e x= " l o o p " f r o m= " 0 " t o= " 4 " s t e p= " 2 "> # l o o p# G o o d M o r n i n g ! . . . i s i t l u n c h t i m e y e t ?< b r > </cfloop> </cfoutput>

Syntax
<cfscript> l o o p = 0; w h i l e( l o o p < 5 ) { W r i t e O u t p u t( " # l o o p # G o o d M o r n i n g ! ") ; W r i t e O u t p u t( " . . . i s i t l u n c h t i m e y e t ? < b r > ") ; l o o p+ +; } </cfscript>

6. Queries
A query is a request to a database. The query can ask for information from the database, write new data to the database, update existing information in the database, or delete records from the database. Each time you query a database with CFML, you get the data (the recordset) and the query variables; together they make up the query object. c f q u e r y passes SQL statements to the "datasource". The "datasource" is set in the ColdFusion administrator. Tag
< c f q u e r y name=" G e t B r e a k f a s t I t e m s " d a t a s o u r c e =" p a n t r y "> SELECT QUANTITY, ITEM FROM CUPBOARD ORDER BY ITEM </cfquery>

Syntax
<cfscript> q u e r y S e r v i c e = new Q u e r y( ) ; q u e r y S e r v i c e . s e t N a m e( " G e t B r e a k f a s t I t e m s ") ; q u e r y S e r v . s e t D a t a s o u r c e( " p a n t r y ") ; q u e r y S e r v i c e . s e t S Q L( " SELECT QUANTITY, ITEM FROM CUPBOARD ORDER BY ITEM ") ; G e t B r e a k f a s t I t e m s = q u e r y S e r v i c e . e x e c u t e( ) .g e t R e s u l t( ) ; </cfscript>

In order to display the data from our query, we need to loop through the rows, and display each row. This is usually done in a < c f o u t p u t > tag like so:
<cfoutput query="GetBreakfastItems"> T h e r e a r e # G e t B r e a k f a s t I t e m s . Q u a n t i t y# # G e t B r e a k f a s t I t e m s . I t e m# i n t h e p a n t r y< b r / > </cfoutput>

While it's not strictly necessary to prepend the recordset name before the column name inside the < c f o u t p u t >, it's strongly recommended that you do in order to prevent referencing the wrong variable scope.

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 6 / 10

You can also loop through a query using standard loop constructs, though they differ when using tags and script. Tag
< c f l o o p q u e r y= " G e t B r e a k f a s t I t e m s "> < c f o u t p u t >T h e r e a r e # G e t B r e a k f a s t I t e m s . Q u a n t i t y# # G e t B r e a k f a s t I t e m s . I t e m# i n t h e p a n t r y< b r / >< / c f o u t p u t > </cfloop>

Syntax
<cfscript> f o r( x = 1 ; x < = G e t B r e a k f a s t I t e m s . R e c o r d C o u n t; x + +) { w r i t e O u t p u t( " T h e r e a r e # G e t B r e a k f a s t I t e m s . Q u a n t i t y [ x ] # # G e t B r e a k f a s t I t e m s . I t e m [ x ] # i n t h e p a n t r y < b r / > ") } </cfscript>

When looping through a query with < c f l o o p >, you need to make sure you have a < c f o u t p u t > tag around your content (or around the loop) to ensure the ColdFusion instructions are recognized. When looping through a query in c f s c r i p t, you'll need to reference the query just like you would a multidimensional array, using the counter set up in your for statement to pick up the correct row from the recordset. So the syntax becomes "recordsetName.ColumnName[rowNumber]".

7. Arrays
Often we need to organize a group and put them into a collection. There are two main types of collections: arrays and structures. An array is a number-indexed list. Picture a city block of houses. Together they form an array and their addresses are the indices. Each house on the block will have a unique address. Some addresses might be empty, but the addresses are all in a specific order. The index is the address of a specific element inside the array. In CFML the index always begins with "1". An array is defined in CFML as an opening "[" then zero or more elements, and a closing "]". Try out this code: Tag
< c f s e t f a v o r i t e _ c o l o r s = [ " r e d ", " b l u e ", " g r e e n ", " b l a c k ", " b r o w n "] < c f d u m p v a r= " # f a v o r i t e _ c o l o r s #" / >< b r > < c f d u m p v a r= " # f a v o r i t e _ c o l o r s [ 2 ] #" / >< b r > < c f d u m p v a r= " # A r r a y L e n ( f a v o r i t e _ c o l o r s ) #" / >< b r > / >

Syntax
<cfscript> f a v o r i t e _ c o l o r s = [ " r e d ", " b l u e ", " g r e e n ", " b l a c k ", " b r o w n "] ; w r i t e D u m p( f a v o r i t e _ c o l o r s) ; w r i t e O u t p u t( " < b r > ") ; w r i t e D u m p( f a v o r i t e _ c o l o r s[ 2 ] ) ; w r i t e O u t p u t( " < b r > ") ; w r i t e D u m p( v a r= A r r a y L e n( f a v o r i t e _ c o l o r s) ) ; </cfscript>

Keep going with these, but try to understand what each instruction is doing before we explain them: Tag
< c f s e t A r r a y A p p e n d( f a v o r i t e _ c o l o r s, " o r a n g e ") < c f s e t f a v o r i t e _ c o l o r s[ 3 ] = " y e l l o w " / > < c f d u m p v a r= " # f a v o r i t e _ c o l o r s #" / >< b r > < c f s e t A r r a y S o r t( f a v o r i t e _ c o l o r s, " t e x t ") / > < c f s e t A r r a y D e l e t e A t( f a v o r i t e _ c o l o r s, 2 ) / > < c f d u m p v a r= " # f a v o r i t e _ c o l o r s #" / >< b r > / >

Syntax
<cfscript> A r r a y A p p e n d( f a v o r i t e _ c o l o r s, " o r a n g e ") ; f a v o r i t e _ c o l o r s[ 3 ] = " y e l l o w "; w r i t e D u m p( f a v o r i t e _ c o l o r s) ; w r i t e O u t p u t( " < b r > ") ; A r r a y S o r t( f a v o r i t e _ c o l o r s, " t e x t ") ; A r r a y D e l e t e A t( f a v o r i t e _ c o l o r s, 2 ) ; w r i t e D u m p( v a r= f a v o r i t e _ c o l o r s) ; w r i t e O u t p u t( " < b r > ") ; </cfscript>

In order to get add an element in the array you use the syntax A r r a y A p p e n d ( a r r a y , " v a l u e " ) or a r r a y n a m e [ i n d e x ] = " v a l u e ". The first example of adding an array element is with an instruction. The second is updating an array element is by assignment. So looking at the final "favorite_colors" array: • What's the index of brown ? • What did the "ArraySort" instruction do to the collection? • What does "ArrayLen" instruction return? There are lots of cool things to do with an array. You can rearrange the order of the elements using the A r r a y S o r t instruction like we did in the last example. You can iterate through each element using the c f l o o p instruction. You can find the address of a specific element by using the "arrayName [index]" instruction. You can ask an array if an element is present with the "ArrayIsDefined" instruction. Try out this example that brings a bunch of things together: Tag
<cfoutput> <ul> < c f l o o p a r r a y= " # f a v o r i t e _ c o l o r s #" i n d e x= " t a r g e t " > <li> # t a r g e t# i s # l e n( t a r g e t) # l e t t e r s l o n g . </li> </cfloop> </ul> < c f d u m p v a r= " # A r r a y I s D e f i n e d ( f a v o r i t e _ c o l o r s , 4 ) #" / > </cfoutput>

Syntax
<cfscript> w r i t e O u t p u t( " < u l > ") ; i n d e x = f a v o r i t e _ c o l o r s . i t e r a t o r( ) ; w h i l e( i n d e x . h a s N e x t( ) ) {

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 7 / 10

t a r g e t = i n d e x . n e x t( ) ; w r i t e O u t p u t( " < l i ># t a r g e t # i s # l e n ( t a r g e t ) # l e t t e r s l o n g . < / l i > ") ; } w r i t e O u t p u t( " < / u l > ") ; w r i t e D u m p( v a r= A r r a y I s D e f i n e d( f a v o r i t e _ c o l o r s, 4 ) ) ; </cfscript>

We use arrays whenever we need a list where the elements are in a specific order.

8. Structures
A structure is a collection of data where each element of data is addressed by a name. As an analogy, think about a classroom of children. Under ideal circumstances, each student has a name and can be found by using that name. We might look in a science classroom for a child named Joey and that would result in finding an actual student. We could write this like "science["Joey"]" which could be read as "look in the collection named "science" and find the thing named "Joey**. A structure is an unordered collection, it’s just a bunch of data collected together where each one has a unique name/key. Structures have a slightly more complicated syntax: Tag
< c f s e t a g e s = { j a c k = 1 1, b r i a n = 1 2, <cfset ages.joey = 12 / > < c f s e t a g e s[ " j i l l "] = 1 4 / > < c f d u m p v a r= " # a g e s #" / > <cfoutput> J o e y i s # a g e s[ " j o e y "] # y e a r s o l d . </cfoutput> t r a c y = 1 1} / >

Syntax
<cfscript> a g e s = { j a c k = 1 1, a g e s . j o e y = 1 2; a g e s[ " j i l l "] = 1 4; b r i a n = 1 2, t r a c y = 1 1} ;

w r i t e D u m p( v a r= a g e s) ; w r i t e O u t p u t( " J o e y i s # a g e s [ " j o e y " ] # y e a r s o l d . ") ; </cfscript>

Here we create a structure named "ages". Structures are made up what are called key-value pairs. The key is used as the address and the value is the object at that address. In the "ages" structure we have keys including joey and jill and values including "12" and "14". When creating a structure using "{}" the key and value are linked by the = symbol. So to create a structure, the structures start with a curly bracket { , have zero or more entries made up of a key, = , and a value separated by commas, then end with a closing curly bracket } . Tag
< c f s e t a g e s[ " j i m m y "] = 1 4 / > < c f s e t a g e s[ " j o e y "] = 9 / > < c f d u m p v a r= " # a g e s #" / >

Syntax
<cfscript> a g e s[ " j i m m y "] = 1 4; a g e s[ " j o e y "] = 9 ; w r i t e D u m p( v a r= a g e s) ; </cfscript>

In the second chunk of the example, we add a new key and value to the structure. Since the jimmy key wasn't in the original structure, it's added with the value of "14". If the key jimmy already existed then the value would be replaced by "14". Every key in a structure must be unique! In the second line we reference the key joey which already exists, so the value gets replaced with the "9". Then, just to show you the state of the structure, we dump out the list of keys and the list of values. Tag
< c f s e t s t u d e n t s = S t r u c t S o r t( a g e s) > < c f l o o p c o l l e c t i o n= " # s t u d e n t s #" i t e m= " s t u d e n t "> < c f o u t p u t >" # s t u d e n t# i s # a g e s[ s t u d e n t] # y e a r s o l d . "< b r / > </cfoutput> </cfloop>

Syntax
students = StructSort (ages); for(student in students) { W r i t e O u t p u t ( " # s t u d e n t # i s # a g e s [ s t u d e n t ] # y e a r s o l d .< b r / >" ) ; }

The last chunk of the example used StructSort to get the sorted array "students" from "ages". Then, it iterated through the "students" array using a loop and gave each element of the array the name "student". It then printed out one line with that student’s name and age from "ages". While that last part probably seemed complicated, it's just to illustrate that structures are unordered.

9. Conditionals
Conditional statements evaluate to "true" or "false" only. The most common conditional operators are = = (equal), ! = (not equal), > (greater than), > = (greater than or equal to), < (less than), and < = (less than or equal to). You can also define the operators as abbreviations: EQ, NEQ, GT, GTE, L T, and LTE. Some instructions return a "true" or "false", so they're used in conditional statements, for example, "IsArray" which is "true" only when the variable is an "array". Structures have an instruction named S t r u c t K e y E x i s t s which returns "true" if a key is present in a structure.

9. 1. If, Else If, & Else

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 8 / 10

Why do we have conditional statements? Most often its to control conditional instructions, especially "if" / "else if" / "else" structures. Lets write an example by adding a method to our PersonalChef class: Tag
< c f f u n c t i o n name= " w a t e r _ b o i l i n g " r e t u r n T y p e= " c o m p o n e n t "> < c f a r g u m e n t name= " m i n u t e s " t y p e= " n u m e r i c " r e q u i r e d= " y e s "> < c f i f ( a r g u m e n t s . m i n u t e s L T 7) > <cfset this.status = "The water is not boiling yet." / > < c f e l s e i f ( a r g u m e n t s . m i n u t e s EQ 7 ) > <cfset this.status = "It's just barely boiling." / > < c f e l s e i f ( a r g u m e n t s . m i n u t e s EQ 8 ) > <cfset this.status = "It's boiling!" / > <cfelse> <cfset this.status = "Hot! Hot! Hot!" / > </cfif> <cfreturn t h i s / > </cffunction>

Syntax
public component function water_boiling(numeric minutes){ i f ( a r g u m e n t s . m i n u t e s < 7) t h i s. s t a t u s = " T h e w a t e r i s n o t b o i l i n g y e t . "; e l s e i f ( a r g u m e n t s. m i n u t e s == 7 ) t h i s. s t a t u s = " I t ' s j u s t b a r e l y b o i l i n g . "; e l s e i f ( a r g u m e n t s. m i n u t e s == 8 ) t h i s. s t a t u s = " I t ' s b o i l i n g ! "; else t h i s. s t a t u s = " H o t ! H o t ! H o t ! "; r e t u r n t h i s; }

Try this example using 5, 7, 8 and 9 for the values of minutes. • When the minutes is 5, here is how the execution goes: Is it true that 5 is less than 7? Yes, it is, so print out the line The water is not boiling yet.. • When the minutes is 7, it goes like this: Is it true that 7 is less than 7? No. Next, is it true that 7 is equal to 7? Yes, it is, so print out the line It's just barely boiling. • When the minutes is 8, it goes like this: Is it true that 8 is less than 7? No. Next, is it true that 8 is equal to 7? No. Next, is it true that 8 is equal to 8? Yes, it is, so print out the line It's boiling!. Lastly, when total is 9, it goes:" Is it "true" that 9 is less than 7? No. Next, is it "true" that 9 is equal to 7? No. Next, is it "true" that 9 is equal to 8? No. Since none of those are true, execute the "else" and print the line "Hot! Hot! Hot!". An "if" block has: • One "if" statement whose instructions are executed only if the statement is true • Zero or more "else if" statements whose instructions are executed only if the statement is true • Zero or one "else" statement whose instructions are executed if no "if" nor "else if" statements were true Only one section of the "if" / "else if" / "else" structure can have its instructions run. If the "if" is "true", for instance, CFML will never look at the "else if". Once one block executes, that’s it.

9. 2. Looping
Another time we use conditional statements is when we want to repeat a set of instructions. Try out this simple example by adding it to your PersonalChef.cfc: Tag
< c f f u n c t i o n name= " c o u n t d o w n " r e t u r n T y p e= " c o m p o n e n t "> < c f a r g u m e n t name= " c o u n t e r " t y p e= " n u m e r i c "> <cfset this.timer = " " / > < c f l o o p c o n d i t i o n= " # a r g u m e n t s . c o u n t e r # G T 0 "> < c f s e t t h i s . t i m e r & = " T h e c o u n t e r i s # a r g u m e n t s . c o u n t e r #. < b r>" < c f s e t a r g u m e n t s . c o u n t e r- * / > < /c f l o o p > <cfreturn t h i s / > </cffunction>

/>

Syntax
public component function countdown (numeric counter){ this.timer = ""; while (counter GT 0) { t h i s . t i m e r &= " T h e c o u n t e r i s # a r g u m e n t s . c o u n t e r # .< b r >" ; a r g u m e n t s . c o u n t e r- ; } return this; }

See how that works? The "counter" starts out as whatever parameter we pass in. The "while" instruction evaluates the conditional statement "arguments.counter GT 0" and finds that yes, the counter is greater than zero. Since the condition is true, execute the instructions inside the loop. First print out The counter is #Arguments.counter# then take the value of "Arguments.counter" and subtract one from it. Next, we overwrite the previous value of "Arguments.counter" with the new value. Then the loop goes back to the "condition" / "while" statement. Is it still true? If so, print the line and subtract one again. Keep repeating until the condition is false. You can also combine conditional statements using logical operators. The most common are known as "logical and" and "logical or". In CFML you can write a "logical and" with either the word "and" or with double ampersands like this: "&&". You can write a "logical or" with the word "or" or with double pipes like this: "||". For each operation, the symbolic representation ( "&&" and "||" ) is more common.

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 9 / 10

The #1 mistake people encounter when writing conditional statements is the difference between = and = =. • = is an assignment. It means "take what's on the right side and stick it into whatever is on the left side" (or its telling not asking.) • = = is a question. It means "is the thing on the right equal to the thing on the left" (or its asking not telling.)

10. Nothingness & Null
What is nothingness? Is there nothingness only in outer space? Really, when we think of nothing isn't it just the absence of something? Ok, that's too much philosophy ColdFusion did not have a way of referring to nothingness until version 9. ColdFusion can receive a "NULL" value from an external source and maintain the "NULL" value until you try to use it. ColdFusion will convert the "NULL" into an empty string (in the case of queries) or potentially destroy the variable altogether. However now with greater support for "NULL" values, ColdFusion allows you to pass in and return a "NULL" value from a method. I s N u l l ( ) instruction will test for "NULL" values and return "true" or "false". If you have three eggs, eat three eggs, then you might think you have nothing , but in terms of eggs you have "0". Zero is something, it's a number, and it's not nothing. A large percentage of the errors you encounter while writing CFML code will involve a variable not existing. You thought something was there, you tried to do something to it, and you can't do something to nothing so CFML creates an error. Lets rewrite our makeEggs method to illustrate "NULL" : Tag
< c f f u n c t i o n name= " m a k e E g g s " r e t u r n T y p e= " c o m p o n e n t "> < c f a r g u m e n t name= " q u a n t i t y " t y p e= " n u m e r i c "> < c f i f ( I s N u l l( a r g u m e n t s . q u a n t i t y) ) / > < c f s e t local.makeEggs = "How am I supposed to make nothingness number of eggs?" / > <cfelse> < c f s e t local.makeEggs = "Making your #arguments.quantity# e g g s ! " / > < c f s e t l o c a l . y o u r E g g s = A r r a y N e w( 1 ) / > < c f l o o p c o n d i t i o n= " # A r r a y L e n ( l o c a l . y o u r E g g s ) # L T # a r g u m e n t s . q u a n t i t y #" > < c f s e t A r r a y A p p e n d( l o c a l . y o u r E g g s, " M a k i n g a n E g g . ") / > </cfloop> </cfif> <cfreturn local / > </cffunction>

Syntax
public component function makeEggs (numeric quantity){ if (IsNull (arguments.quantity)) { local.makeEggs = "How am I supposed to make nothingness number of eggs?"; } else { local.makeEggs = "Making your #arguments.quantity# eggs!"; local.yourEggs = ArrayNew (1); w h i l e ( A r r a y L e n ( l o c a l . y o u r E g g s ) < a r g u m e n t s. q u a n t i t y) A r r a y A p p e n d ( l o c a l. y o u r E g g s, " M a k i n g a n E g g. " ) ; } r e t u r n l o c a l; }

Reload the file, call f r a n k . m a k e E g g s ( 3 ) then try f r a n k . m a k e E g g s ( ). TODO: Conclusion

GitHub About Blog Features Contact & Support Training GitHub Enterprise Site Status

Tools Gauges: Analyze web traffic Speaker Deck: Presentations Gist: Code snippets GitHub for Mac Issues for iPhone Job Board

Extras GitHub Shop The Octodex

Documentation GitHub Help Developer API GitHub Flavored Markdown GitHub Pages

Terms of Service Privacy Security © 2012 GitHub Inc. All rights reserved.

Powered by the Dedicated Servers and Cloud Computing of Rackspace Hosting®

https://github.com/mhenke/CFML-in-100-minutes/blob/master/cfml100mins.markdown

Page 10 / 10

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close