Skip to content
This repository
Newer
Older
100644 952 lines (703 sloc) 35.224 kb
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
1 # CFML in 100 minutes
2
3 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.
4
5 1. Syntax
6 2. Variables
7 3. Components, Methods, and Parameters
8 4. Strings
9 5. Numbers
10 6. Queries
11 7. Arrays
12 8. Structures
13 9. Conditionals
14 1. If, Else If, & Else
15 2. Looping
16
17 10. Nothingness & Null
18
19 ## CFML History
20
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
21 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](http://www.adobe.com/coldfusion) originated as proprietary technology, however, the availability of competing open source products like [Railo](http://www.getrailo.com/) and [OpenBD](http://www.openbluedragon.org/) has made CFML more widely available.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
22
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
23 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.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
24
25 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.
26
27 And you want to learn CFML so here goes!
28
29 ## 1. Syntax
30
31 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.
32
33 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.
34
35 We might have a file named _myprogram.cfm_ and _Sample.cfc_ like this:
36
37 ### Tag Syntax
38
39 #### myprogram.cfm
40
41 ```cfm
42 <cfset s = New Sample() />
43 <cfoutput>#s.hello()#</cfoutput>
44 ```
45
46 #### Sample.cfc
47
48 ```cfm
49 <cfcomponent>
50 <cffunction name="hello">
51 <cfreturn "Hello, World!" />
52 </cffunction>
53 </cfcomponent>
54 ```
55
56 ### Script Syntax
57
58 #### myprogram.cfm
59
60 ```cfm
61 <cfscript>
62 s = New Sample();
63 writeOutput(s.hello());
64 </cfscript>
65 ```
66
67 #### Sample.cfc
68
69 ```cfm
70 component {
71 public string function hello(){
72 return( "Hello, World!" );
73 }
74 }
75 ```
76
77 For the script example, _myprogram.cfm_ would have beginning/closing ```<cfscript>``` tags around the instructions, however, the script-based _Sample.cfc_ does not require ```<cfscript>``` tags around the instructions.
78
79 ### PHP Syntax
80
81 #### myprogram.php
82
83 ```php
84 <?php
85 require("Sample.php");
86 $s = new Sample();
3732147d » busches
2011-07-11 Edited cfml100mins.markdown via GitHub
87 echo $s->hello();
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
88 ?>
89 ```
90
91 #### Sample.php
92
93 ```php
94 <?php
95 class Sample
96 {
97 public function hello() {
98 return "Hello, World!";
99 }
100 }
101 ?>
102 ```
103
104 ### Ruby Syntax
105
106 #### myprogram.rb
107
108 ```rb
109 class Sample
110 def hello
111 "Hello, World!"
112 end
113
114 s = Sample.new
115 puts s.hello
116 ```
117
118 ## 2. Variables
119
120 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.
121
122 Go into a CFML file, enter in these example instructions, and observe the output that CFML gives you back:
123
124 #### Tag
125
126 ```cfm
127 <cfoutput>
128 <cfset a = 5 />
129 a = #a#<br/>
130 <cfset b = 10 + 5 />
131 b = #b#<br/>
132 <cfset c = 15 + a + b />
133 c = #c#<br/>
134 <cfset b = c * a />
135 b = #b#<br/>
136 <cfset d = "Hello, " />
137 d = #d#<br/>
138 </cfoutput>
139 ```
140
141 #### Syntax
142
143 ```cfm
144 <cfscript>
145 a = 5;
146 writeOutput("a = #a#<br/>");
147 b = 10 + 5;
148 writeOutput("b = #b#<br/>");
149 c = 15 + a + b;
150 writeOutput("c = #c#<br/>");
151 b = c * a;
152 writeOutput("b = #b#<br/>");
153 d = "Hello, ";
154 writeOutput("d = #d#<br/>");
155 </cfscript>
156 ```
157
158 In this second example, we assume the first example is present.
159
160 #### Tag
161
162 ```cfm
163 <cfoutput>
164 <cfset e = "World!" />
165 e = #e#<br/>
166 <cfset f = d & e />
167 f = #f#<br/>
168 <cfset g = d & a & e />
169 g = #g#<br/>
170 <cfset b = "hi!" />
171 b = #b#<br/>
172 </cfoutput>
173 ```
174
175 #### Syntax
176
177 ```cfm
178 <cfscript>
179 e = "World!";
180 writeOutput("e = #e#<br/>");
181 f = d & e;
182 writeOutput("f = #f#"<br/>");
183 g = d & a & e;
184 writeOutput("g = #g#<br/>");
185 b = "hi!";
186 writeOutput("b = #b#<br/>");
187 </cfscript>
188 ```
189
190 *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.
191
192 ## 3. Components, Methods, and Parameters
193
194 ### Components (aka Objects or Classes)
195
196 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 ```cffunction``` tag is used to define methods within a tag-based CFC. In a script-based CFC, you use the ```function``` keyword to define a method.
197
198 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.
199
200 In CFML we define an object using the ```cfcomponent``` instruction (```component``` in script-based CFCs) and save the file as _.cfc_. Here's an example defining the object type _PersonalChef.cfc_:
201
202 #### Tag
203
204 ```cfm
205 <cfcomponent>
206
207 </cfcomponent>
208 ```
209
210 #### Syntax
211
212 ```cfm
213 component {
214
215 }
216 ```
217
218 ### Methods
219
220 Inside the CFC we usually define one or more methods using the ```cffunction``` or ```function``` instruction like this:
221
222 #### Tag
223
224 ```cfm
225 <cfcomponent>
226 <cffunction name="makeToast">
227 <cfset makeToast = "Making your toast!" />
228 </cffunction>
229 </cfcomponent>
230 ```
231
232 #### Syntax
233
234 ```cfm
235 component {
236 public string function makeToast(){
237 makeToast = "Making your toast!";
238 }
239 }
240 ```
241
242 Inside the ```cffunction```/```function``` instruction we would put the code for how the chef should make the toast.
243
244 ### Classes
245
3732147d » busches
2011-07-11 Edited cfml100mins.markdown via GitHub
246 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.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
247
248 Once we define a class, we create an instance of that class like this:
249
250 #### Tag
251
252 ```cfm
253 <cfset frank = New PersonalChef() />
254 ```
255
256 #### Syntax
257
258 ```cfm
259 frank = New PersonalChef();
260 ```
261
262 We're calling the ```New``` instruction on the class ```PersonalChef``` 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: ```object.method_name()```. So if you have a person named "frank" you would tell him to make toast by calling ```frank.makeToast()```.
263
6277685c » lazorstudios
2011-07-11 Some simple spelling/grammar fixes.
264 The ```New``` instruction creates a new instance of the object and calls its ```init()``` method (if existing). Any arguments supplied to the object will be passed to the ```init()``` method. The ```init()``` method should return the object instance using ```<cfreturn this />``` in order to have the same expected behavior as the ```CreateObject``` instruction. If no ```init()``` method exists, the object will be returned normally.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
265
266 ### Method Parameters
267
6277685c » lazorstudios
2011-07-11 Some simple spelling/grammar fixes.
268 Sometimes methods take one or more *parameters* telling them **how** to do what they're supposed to **do**. For instance, I might call ```frank.makeToast("burned")``` for him to burn my toast. Or maybe he has another method where I call ```frank.makebreakfast("toast","eggs")``` 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 ```cfargument``` instruction, it'll look like this:
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
269
270 #### Tag
271
272 ```cfm
273 <cfcomponent>
274 <cffunction name="makeToast" returnType="string">
275 <cfargument name="color" required="yes">
276 <cfset makeToast = "Making your toast #arguments.color#!" />
277 </cffunction>
278 </cfcomponent>
279 ```
280
281 #### Syntax
282
283 ```cfm
284 component {
285 public string function makeToast(required String color){
286 makeToast = "Making your toast #arguments.color#!";
287 }
288 }
289 ```
290
291 The method is requiring us to pass in a "color" telling it how to do the method "makeToast".
292
293 ### Return Value
294
295 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 ```makeToast``` method above like ```<cfset result = frank.makeToast("burned") />``` or ```set result = frank.makeToast("burned");```, and tried to output "result" you should have seen "Variable RESULT is undefined".
296
297 To return data, we use ```cfreturn``` to instruct the method to return a "value". Since that wasn't in the last instruction before the ending ```cffunction``` in your ```makeToast``` method, you received *nothing* and tried to putting that into the "result" variable.
298
6277685c » lazorstudios
2011-07-11 Some simple spelling/grammar fixes.
299 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:
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
300
301 #### Tag
302
303 ```cfm
304 <cfcomponent>
305 <cffunction name="makeToast" returnType="component">
306 <cfargument name="color" required="yes">
307 <cfset this.makeToast = "Making your toast #arguments.color#!" />
308 <cfreturn this />
309 </cffunction>
310 </cfcomponent>
311 ```
312
313 #### Syntax
314
315 ```cfm
316 component {
317 public component function makeToast(required String color){
318 this.makeToast = "Making your toast #arguments.color#!";
319 return this;
320 }
321 }
322 ```
323
324 ## 4. Strings
325
13815e61 » pfreitag
2011-07-11 Changed strings section to make clear that both single and double quo…
326 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.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
327
328 ### Len
329 * Call ```Len``` on a string to get back the number of characters in the string. For instance ```Len("Hello")``` would give you back **5**.
330
331 ### Replace
332 * The ```Replace``` 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, ```Replace("Hello", "e", "")``` would give you back **hllo** after replacing the _first occurrence of e_, or ```Replace("Good Morning!", "o", "e", "All")``` would give you **Geed Merning!**
333
334 ### RemoveChars
335 * Call ```RemoveChars``` to remove characters from a string. For instance, ```RemoveChars("hello bob", 2, 5)``` would give you back **hbob**.
336
337 ### Mid
338
339 * The ```mid``` instruction extracts a substring from a string. For instance, I could call ```Mid("Welcome to CFML Jumpstart",4,12)``` and it would give you back: **come to CFML**.
340
341 Experiment with the following samples in a CFML file.
342
343 #### Tag
344
345 ```cfm
346 <cfset tester = "Good Morning Everyone!" />
347 <cfoutput>#len (tester)#<br></cfoutput>
348 <cfoutput>#Replace (tester, "o", "e", "All")#<br></cfoutput>
349 <cfoutput>#RemoveChars (tester, 2, 5)#<br></cfoutput>
350 <cfset t2 = "sample,data,from,a,CSV" />
351 <cfset t3 = Mid(t2,8,len(t2)) />
352 <cfoutput>#t3#<br></cfoutput>
353 ```
354
355 #### Syntax
356
357 ```cfm
358 <cfscript>
359 tester = "Good Morning Everyone!";
360 writeOutput ("#len (tester)#<br/>");
361 writeOutput (Replace (tester, "o", "e", "All") & "<br/>");
362 writeOutput (RemoveChars (tester, 2, 5) & "<br/>");
363 t2 = "sample,data,from,a,CSV";
364 t3 = Mid (t2,8,len (t2));
365 writeOutput (t3 & "<br/>");
366 </cfscript>
367 ```
368
369 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:
370
371 #### Tag
372
373 ```cfm
374 <cfset t4 = ListToArray(t3) />
375 <cfoutput>
376 #t4[2]#
377 </cfoutput>
378 ```
379
380 #### Syntax
381
382 ```cfm
383 <cfscript>
384 t4 = ListToArray(t3);
385 writeOutput(t4[2]);
386 </cfscript>
387 ```
388
389 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.
390
391 ### Combining Strings and Variables
392
393 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:
394
395 **Happy Saturday!**
396
397 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.
398
399 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:
400
401 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:
402
403 ```cfm
404 today = DayOfWeek(Now());
405 ```
406
407 ```Now()``` 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:
408
409 #### Tag
410
411 ```cfm
412 <cfset today = DayOfWeekAsString(DayOfWeek(Now())) />
413 <cfset message = "Happy " & today & "!" />
414 <cfoutput>
415 #message#
416 </cfoutput>
417 ```
418
419 #### Syntax
420
421 ```cfm
422 <cfscript>
423 today = DayOfWeekAsString(DayOfWeek(Now()));
424 message = "Happy " & today & "!";
425 writeOutput(message);
426 </cfscript>
427 ```
428
429 Great, no errors and our output looks correct. "DayOfWeekAsString" did the trick. There is another string combination called *string interpolation*.
430
431 **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:
432
433 #### Tag
434
435 ```cfm
436 <cfset message = "Happy #today#!" />
437 ```
438
439 #### Syntax
440
441 ```cfm
442 message = "Happy #today#!";
443 ```
444
445 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.
446
447 Basically *interpolating* means evaluate the code inside this ```#``` wrapper and put it into the string.
448
449 ## 5. Numbers
450
451 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:
452
453 #### Tag
454
455 ```cfm
456 <cfset loop = 0 />
457 <cfoutput>
458 <cfloop condition="loop LT 5" >
459 #loop# Hello, world!<br>
460 <cfset loop++ />
461 </cfloop>
462 I am here!<br>
463 </cfoutput>
464 ```
465
466 #### Syntax
467
468 ```cfm
469 <cfscript>
470 for (loop = 0 ; loop < 5 ; loop++)
471 WriteOutput("#loop# Hello, world!<br>");
472 WriteOutput("I am here<br>");
473 </cfscript>
474 ```
475
476 In this next example we're using the ```cfloop``` 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.
477
478 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 ```from``` and ```to``` arguments, and ColdFusion will simply loop from the first value to the second, and automatically increment the variable in the ```index``` argument.
479
480 Try this example with multiple instructions:
481
482 #### Tag
483
484 ```cfm
485 <cfset loop = 0 />
486 <cfoutput>
487 <cfloop index="loop" from="0" to="4">
488 #loop# Good Morning!
489 ...is it lunch time yet?<br>
490 </cfloop>
491 </cfoutput>
492 ```
493
494 #### Syntax
495
496 ```cfm
497 <cfscript>
498 loop = 0;
499 while (loop < 5) {
500 WriteOutput("#loop# Good Morning! ");
501 WriteOutput("...is it lunch time yet?<br>");
502 loop++;
503 }
504 </cfscript>
505 ```
506
507 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.
508
509 #### Tag
510
511 ```cfm
512 <cfset loop = 0 />
513 <cfoutput>
514 <cfloop index="loop" from="0" to="4" step="2">
515 #loop# Good Morning!
516 ...is it lunch time yet?<br>
517 </cfloop>
518 </cfoutput>
519 ```
520
521 #### Syntax
522
523 ```cfm
524 <cfscript>
525 loop = 0;
526 while (loop < 5) {
527 WriteOutput("#loop# Good Morning! ");
528 WriteOutput("...is it lunch time yet?<br>");
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
529 loop++;
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
530 }
531 </cfscript>
532 ```
533
534 ## 6. Queries
535
536 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. ```cfquery``` passes SQL statements to the "datasource". The "datasource" is set in the ColdFusion administrator.
537
538 #### Tag
539
540 ```cfm
541 <cfquery name="GetBreakfastItems" datasource="pantry">
542 SELECT QUANTITY, ITEM
543 FROM CUPBOARD
544 ORDER BY ITEM
545 </cfquery>
546 ```
547
548 #### Syntax
549
550 ```cfm
551 <cfscript>
552 queryService = new Query ();
553
554 queryService.setName("GetBreakfastItems");
555 queryServ.setDatasource("pantry");
556 queryService.setSQL("
557 SELECT QUANTITY, ITEM
558 FROM CUPBOARD
559 ORDER BY ITEM
560 ");
561
562 GetBreakfastItems = queryService.execute().getResult();
563 </cfscript>
564 ```
565
566 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 ```<cfoutput>``` tag like so:
567
568 ```cfm
569 <cfoutput query="GetBreakfastItems">
570 There are #GetBreakfastItems.Quantity# #GetBreakfastItems.Item# in the pantry<br />
571 </cfoutput>
572 ```
573
574 While it's not strictly necessary to prepend the recordset name before the column name inside the ```<cfoutput>```, it's strongly recommended that you do in order to prevent referencing the wrong variable scope.
575
576 You can also loop through a query using standard loop constructs, though they differ when using tags and script.
577
578 #### Tag
579
580 ```cfm
581 <cfloop query="GetBreakfastItems">
582 <cfoutput>There are #GetBreakfastItems.Quantity# #GetBreakfastItems.Item# in the pantry<br /></cfoutput>
583 </cfloop>
584 ```
585
586 #### Syntax
587
588 ```cfm
589 <cfscript>
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
590 for (x = 1; x <= GetBreakfastItems.RecordCount; x++) {
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
591 writeOutput("There are #GetBreakfastItems.Quantity[x]# #GetBreakfastItems.Item[x]# in the pantry<br />")
592 }
593 </cfscript>
594 ```
595
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
596 When looping through a query with ```<cfloop>```, you need to make sure you have a ```<cfoutput>``` tag around your content (or around the loop) to ensure the ColdFusion instructions are recognized.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
597
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
598 When looping through a query in ```cfscript```, 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]".
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
599
600 ## 7. Arrays
601
602 Often we need to organize a group and put them into a *collection*. There are two main types of collections: **arrays** and **structures**.
603
604 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:
605
606 #### Tag
607
608 ```cfm
609 <cfset favorite_colors = ["red","blue","green","black","brown"] />
610 <cfdump var="#favorite_colors#" /><br>
611 <cfdump var="#favorite_colors[2]#" /><br>
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
612 <cfdump var="#ArrayLen(favorite_colors)#" /><br>
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
613 ```
614
615 #### Syntax
616
617 ```cfm
618 <cfscript>
619 favorite_colors = ["red","blue","green","black","brown"];
620 writeDump(favorite_colors);
621 writeOutput("<br>");
622 writeDump(favorite_colors[2]);
623 writeOutput("<br>");
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
624 writeDump(var=ArrayLen(favorite_colors));
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
625 </cfscript>
626 ```
627
628 Keep going with these, but try to understand what each instruction is doing before we explain them:
629
630 #### Tag
631
632 ```cfm
633 <cfset ArrayAppend(favorite_colors, "orange") />
634 <cfset favorite_colors[3]="yellow" />
635 <cfdump var="#favorite_colors#" /><br>
636 <cfset ArraySort(favorite_colors,"text") />
637 <cfset ArrayDeleteAt(favorite_colors, 2) />
638 <cfdump var="#favorite_colors#" /><br>
639 ```
640
641 #### Syntax
642
643 ```cfm
644 <cfscript>
645 ArrayAppend(favorite_colors, "orange");
646 favorite_colors[3] = "yellow";
647 writeDump(favorite_colors);
648 writeOutput("<br>");
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
649 ArraySort(favorite_colors,"text");
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
650 ArrayDeleteAt(favorite_colors, 2);
651 writeDump(var=favorite_colors);
652 writeOutput("<br>");
653 </cfscript>
654 ```
655
656 In order to get add an element in the array you use the syntax ```ArrayAppend(array,"value")``` or ```arrayname[index] = "value"```. 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:
657
658 * What's the index of **brown** ?
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
659 * What did the "ArraySort" instruction do to the collection?
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
660 * What does "ArrayLen" instruction return?
661
662 There are lots of cool things to do with an array. You can rearrange the order of the elements using the ```ArraySort``` instruction like we did in the last example. You can iterate through each element using the ```cfloop``` 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:
663
664 #### Tag
665
666 ```cfm
667 <cfoutput>
668 <ul>
669 <cfloop array="#favorite_colors#" index="target" >
670 <li>
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
671 #target# is #len(target)# letters long.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
672 </li>
673 </cfloop>
674 </ul>
675 <cfdump var="#ArrayIsDefined(favorite_colors,4)#" />
676 </cfoutput>
677 ```
678
679 #### Syntax
680
681 ```cfm
682 <cfscript>
683 writeOutput ("<ul>");
684 index = favorite_colors.iterator ();
685 while (index.hasNext ()){
686 target = index.next ();
687 writeOutput ("<li>#target# is #len (target)# letters
688 long.</li>");
689 }
690 writeOutput ("</ul>");
691 writeDump (var=ArrayIsDefined (favorite_colors,4));
692 </cfscript>
693 ```
694
695 We use arrays whenever we need a list where the elements are in a specific order.
696
697 ## 8. Structures
698
699 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**.
700
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
701 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:
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
702
703 #### Tag
704
705 ```cfm
706 <cfset ages = {jack = 11, brian = 12, tracy = 11} />
707 <cfset ages.joey = 12 />
708 <cfset ages["jill"] = 14 />
709
710 <cfdump var="#ages#" />
711
712 <cfoutput>
713 Joey is #ages["joey"]# years old.
714 </cfoutput>
715 ```
716
717 #### Syntax
718
719 ```cfm
720 <cfscript>
721 ages = {jack = 11, brian = 12, tracy = 11};
722 ages.joey = 12;
723 ages["jill"] = 14;
724
725 writeDump (var=ages);
726 writeOutput ("Joey is #ages["joey"]# years old.");
727 </cfscript>
728 ```
729
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
730 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 ```}```.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
731
732 #### Tag
733
734 ```cfm
735 <cfset ages["jimmy"] = 14 />
736 <cfset ages["joey"] = 9 />
737 <cfdump var="#ages#" />
738 ```
739
740 #### Syntax
741
742 ```cfm
743 <cfscript>
744 ages["jimmy"] = 14;
745 ages["joey"] = 9;
746 writeDump (var=ages);
747 </cfscript>
748 ```
749
750 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.
751
752 #### Tag
753
754 ```cfm
755 <cfset students = StructSort(ages)>
756
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
757 <cfloop collection="#students#" item="student">
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
758 <cfoutput>"#student# is #ages[student]# years old."<br />
759 </cfoutput>
760 </cfloop>
761 ```
762
763 #### Syntax
764
765 ```cfm
766 students = StructSort (ages);
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
767 for(student in students) {
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
768 WriteOutput ("#student# is #ages[student]# years old.<br />");
e0cd304e » donquist
2011-07-11 Edited cfml100mins.markdown via GitHub
769 }
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
770 ```
771
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
772 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".
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
773
774 While that last part probably seemed complicated, it's just to illustrate that structures are unordered.
775
776 ## 9. Conditionals
777
778 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```, ```LT```, and ```LTE```.
779
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
780 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 ```StructKeyExists``` which returns "true" if a key is present in a structure.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
781
782 ### 9. 1. If, Else If, & Else
783
784 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:
785
786 #### Tag
787
788 ```cfm
789 <cffunction name="water_boiling" returnType="component">
790 <cfargument name="minutes" type="numeric" required="yes">
791
792 <cfif (arguments.minutes LT 7)>
793 <cfset this.status = "The water is not boiling yet." />
794 <cfelseif (arguments.minutes EQ 7)>
795 <cfset this.status = "It's just barely boiling." />
796 <cfelseif (arguments.minutes EQ 8)>
797 <cfset this.status = "It's boiling!" />
798 <cfelse>
799 <cfset this.status = "Hot! Hot! Hot!" />
800 </cfif>
801 <cfreturn this />
802 </cffunction>
803 ```
804
805 #### Syntax
806
807 ```cfm
808 public component function water_boiling(numeric minutes){
809 if (arguments.minutes < 7)
810 this.status = "The water is not boiling yet.";
811
812 else if (arguments.minutes == 7)
813 this.status = "It's just barely boiling.";
814
815 else if (arguments.minutes == 8)
816 this.status = "It's boiling!";
817
818 else
819 this.status = "Hot! Hot! Hot!";
820
821 return this;
822 }
823 ```
824
825 Try this example using *5*, *7*, *8* and *9* for the values of *minutes*.
826
827 * 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.*.
828
829 * 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*.
830
831 * 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!*.
832
833 Lastly, when total is 9, it goes:" Is it "true" that 9 is less than 7?
834
835 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!".
836
837 An "if" block has:
838
839 * One "if" statement whose instructions are executed only if the
840 statement is true
841 * Zero or more "else if" statements whose instructions are executed
842 only if the statement is true
843 * Zero or one "else" statement whose instructions are executed if no
844 "if" nor "else if" statements were true
845
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
846 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.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
847
848 ### 9. 2. Looping
849
850 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_:
851
852 #### Tag
853
854 ```cfm
855 <cffunction name="countdown" returnType="component">
856 <cfargument name="counter" type="numeric">
857 <cfset this.timer = "" />
858 <cfloop condition="#arguments.counter# GT 0">
859 <cfset this.timer &= "The counter is #arguments.counter#.<br>" />
860 <cfset arguments.counter-* />
861 </cfloop>
862 <cfreturn this />
863 </cffunction>
864 ```
865
866 #### Syntax
867
868 ```cfm
869 public component function countdown (numeric counter){
870 this.timer = "";
871 while (counter GT 0) {
872 this.timer &= "The counter is #arguments.counter#.<br>";
873 arguments.counter-;
874 }
875 return this;
876 }
877 ```
878
879 See how that works? The "counter" starts out as whatever parameter we
880 pass in. The "while" instruction evaluates the conditional statement
881 "arguments.counter GT 0" and finds that yes, the counter is greater than
882 zero. Since the condition is true, execute the instructions inside the
883 loop. First print out **The counter is #Arguments.counter#** then take
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
884 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
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
885 "condition" / "while" statement. Is it still true? If so, print the line
886 and subtract one again. Keep repeating until the condition is false.
887
888 You can also combine conditional statements using logical operators. The
889 most common are known as "logical and" and "logical or". In CFML you can
890 write a "logical and" with either the word "and" or with double
891 ampersands like this: "&&". You can write a "logical or" with the word
892 "or" or with double pipes like this: "||". For each operation, the
893 symbolic representation ( "&&" and "||" ) is more common.
894
895 The #1 mistake people encounter when writing conditional statements is
896 the difference between ```=``` and ```==```.
897
898 * ```=``` is an *assignment*. It means "take what's on the right side and
899 stick it into whatever is on the left side" (or its *telling* not
900 *asking*.)
901 * ```==``` is a *question*. It means "is the thing on the right equal to
902 the thing on the left" (or its *asking* not *telling*.)
903
904 ## 10. Nothingness & Null
905
906 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
907
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
908 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. ```IsNull()``` instruction will test for "NULL" values and return "true" or "false".
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
909
537499dc » busches
2011-07-11 Fix a few typos.
910 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*.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
911
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
912 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" :
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
913
914 #### Tag
915
916 ```cfm
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
917 <cffunction name="makeEggs" returnType="component">
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
918 <cfargument name="quantity" type="numeric">
919 <cfif (IsNull(arguments.quantity)) />
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
920 <cfset local.makeEggs = "How am I supposed to make nothingness number of eggs?" />
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
921 <cfelse>
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
922 <cfset local.makeEggs = "Making your #arguments.quantity# eggs!" />
923 <cfset local.yourEggs = ArrayNew(1) />
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
924
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
925 <cfloop condition="#ArrayLen(local.yourEggs)# LT #arguments.quantity#">
926 <cfset ArrayAppend(local.yourEggs, "Making an Egg.") />
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
927 </cfloop>
928 </cfif>
11c1d3b8 » designfrontier
2011-07-12 missed one instance of this that needed to be replaced by local
929 <cfreturn local />
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
930 </cffunction>
931 ```
932
933 #### Syntax
934
935 ```cfm
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
936 public component function makeEggs (numeric quantity){
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
937 if (IsNull (arguments.quantity)) {
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
938 local.makeEggs = "How am I supposed to make nothingness number of
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
939 eggs?";
940 } else {
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
941 local.makeEggs = "Making your #arguments.quantity# eggs!";
942 local.yourEggs = ArrayNew (1);
943 while (ArrayLen (local.yourEggs) < arguments.quantity)
944 ArrayAppend (local.yourEggs, "Making an Egg.");
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
945 }
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
946 return local;
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
947 }
948 ```
949
f0598e5e » designfrontier
2011-07-12 Changed the this scope to the local scope in the makeEggs() method in…
950 Reload the file, call ```frank.makeEggs(3)``` then try ```frank.makeEggs()```.
23c087c9 » imageaid
2011-07-05 Mike, I made a few editorial changes up through the components sectio…
951
9d272fd3 » designfrontier
2011-07-12 Ran it through Microsoft Word spelling and grammar check. Rewrote a f…
952 **TODO: Conclusion**
Something went wrong with that request. Please try again.