Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
learncfinaweek / gist:4121071
Created November 20, 2012 21:00
Decision Making and Scopes - Decision Making

An important part of any program is the ability to make decisions based upon a set of conditions. This is aptly named 'conditional logic'. Unless you are writing a static set of HTML pages, you will almost certainly need to evaluate conditions. In conditional logic, all conditions tested must evaluate to a Boolean, either true or false. As you learned in the variables section of this training, ColdFusion is a dynamically typed language. This does not mean that ColdFusion is not typed, but rather that ColdFusion can switch from type to type dynamically while the application is executing. This is a great feature when using conditional logic as you do not need to explicitly define the Boolean such as myVar == true, but for simple variables you can allow ColdFusion to evaluate the value of the variable and convert it dynamically to a Boolean value. Keep this in mind, and we will revisit this in a bit.

ColdFusion has both if/then/else tags

@learncfinaweek
learncfinaweek / gist:4121080
Created November 20, 2012 21:00
Decision Making and Scopes - Scopes

ColdFusion groups variables together in scopes, or a range in which a variable can be accessed. Think of a scope as a bucket of memory that can store variables. Each scope has its own purpose, and each scope has its own lifecycle.

The following table shows major scopes available in a running ColdFusion application:

  • Variables: Default scope available in ColdFusion templates. Variables are available only during the execution of the template.
@learncfinaweek
learncfinaweek / gist:4121092
Created November 20, 2012 21:02
Decision Making and Scopes - Hands On 3
<p>
In this hands on, you are going to add some conditional logic to the site as well as create a form post.
</p>
<p>
<strong>Tags Used</strong>: <a href="http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fe8.html" target="_new">&lt;cfif></a>, <a href="http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7faf.html" target="_new">&lt;cfparam> </a>
</p>
<p>
<ol>
<li>
Open up the <span class="code">/www/contact.cfm</span> file in your code editor.
@learncfinaweek
learncfinaweek / gist:4121096
Created November 20, 2012 21:02
Decision Making and Scopes - Hands On 4

In this hands on, you are going to validate the form data that was submitted and output any problems that might have occurred. We will access data from the form scope.

Tags Used: <cfset>, <cfif>

Functions Used: len

@learncfinaweek
learncfinaweek / gist:4121101
Created November 20, 2012 21:03
Decision Making and Scopes - Hands On 5

In this hands on, you are going to update our code to provide a better user experience to your users as well as improve the form validation.

Tags Used: <cfparam>, <cfoutout>

Functions Used: len, trim

@learncfinaweek
learncfinaweek / gist:4121103
Created November 20, 2012 21:04
Decision Making and Scopes - Hands On 6

In this hands on, we are going to refactor our code and take out any unnecessary logic we might have.

Tags Used: <cfif>, <cfelse>

  1. Open up the /www/contact.cfm file in your code editor and locate the <cfif> statement that checks the form.contactname variable. Remove the eq 0 part of the expression; as any number greater than 0 is treated as true, and the number 0 is treated as false, we do not need this additional part of the <cfif> statement. Because we only want to run the code when there is NO value passed we must negate the value by putting the word NOT before

Looping is employed by programming languages to repeat a behavior defined by a section of code either while a condition is maintained true (conditional looping), or a certain number of times (iterative looping). As with other programming languages, ColdFusion allows you to perform both iterative and conditional looping.

ColdFusion uses the cfloop tag to enable looping in tag-based syntax. cfloop permits you to loop over a variable such as a structure or array, loop while a condition remains true, or loop for a given iteration. ColdFusion permits looping in cfscript using for loops and do while loops.

Tag-based Looping

@learncfinaweek
learncfinaweek / gist:4121113
Created November 20, 2012 21:05
Looping - Hands On 7

In this hands on, we will perform some simple looping using a list and output it to the page.

Tags Used: <cfset>, <cfloop>, <cfoutput>

  1. Open up the /www/resume.cfm file in your code editor.
@learncfinaweek
learncfinaweek / gist:4121117
Created November 20, 2012 21:05
Looping - Hands On 8
@learncfinaweek
learncfinaweek / gist:4121125
Created November 20, 2012 21:06
Data Handling - Databases

One of the key parts of any application is its ability to do CRUD (Create, Read, Update, and Delete) operations. ColdFusion has the ability to read a variety of data sources with minimal effort; there is built in support for over ten different databases. Among the supported databases, you will find MS SQL, MySql, Oracle, DB2, and Sybase. There are slight sql syntax differences when interacting with the database; however, the same code can be used against different databases without any changes.

Note: This chapter will not teach you how to write sql. However, it will show you how to take the sql skills you have and incorporate them into your code. Alternatively, if you are using ColdFusion Builder, you can use the query builder to assist you with query creation.

Query Basics