Book Image

ColdFusion 9 Developer Tutorial

By : John Farrar
Book Image

ColdFusion 9 Developer Tutorial

By: John Farrar

Overview of this book

Adobe ColdFusion is an application server, renowned for rapid development of dynamic websites, with a straightforward language (CFML), powerful methods for packaging and reusing your code, and AJAX support that will get developers deep into powerful web applications quickly. However, developing rich and robust web applications can be a real challenge as it involves multiple processes.With this practical guide, you will learn how to build professional ColdFusion applications. Packed with example code, and written in a friendly, easy-to-read style, this book is just what you need if you are serious about ColdFusion.This book will give you clear, concise, and practical guidance to take you from the basics of ColdFusion 9 to the skills that will make you a ColdFusion developer to be reckoned with. It also covers the new features of ColdFusion 9 like ORM Database Interaction and CF Builder.ColdFusion expert John Farrar will teach you the basics of ColdFusion programming, application architecture, and object reuse, before showing you a range of topics including AJAX library integration, RESTful Web Services, PDF creation and manipulation, and dynamically generated presentation files that will make you the toast of your ColdFusion developer town.This book digs deep with the basics, with real-world examples of the how and whys, to get more done faster with ColdFusion 9.
Table of Contents (22 chapters)
ColdFusion 9 Developer Tutorial
Credits
About the Author
About the Reviewers
Preface
Index

Conditional processing with If/<cfif>


Now, we have reached the last segment of our fast track introduction to ColdFusion. We would be amiss if we were to forget conditional processing. There are two tags which make up the bulk of conditional processing in ColdFusion.

The first tag is <cfIf>. The second is <cfelse> which has a variation called <cfelseif> where the functionality of the two is combined. If you are familiar with any other language, writing code in ColdFusion should be very similar. The only out of the ordinary issue is that when coding with tags, the language needs to be able to tell the difference between tag braces and greater and less than logic. In ColdFusion tag requests, this is done by replacing the greater than symbol with GT. We replace the less than symbol with LT. We use GTE and LTE for greater than or equal to and less than or equal to, respectively. If something is equal we can use either IS or EQ.

Now that we have covered these concepts, let's get to something fun. Let's look at some code! What can we do to make sure someone doesn't fool around with the URL and make our page fail by changing URL variables? Let's put some conditional processing in to perform business logic in the processing section of our pages:

<!--- Example: 1_20.cfm --->
<!--- Processing --->
<cfparam name="url.faq" default="1">

<cfscript>
faq = arrayNew(1);
  faq[1] = structNew();
  faq[1].question = "What is the speed limit?";
  faq[1].answer = "55";	
  faq[2] = structNew();
  faq[2].question = "What is a car?";
  faq[2].answer = "Depends who you ask!";
  faq[3] = structNew();
  faq[3].question = "How much is gas?";
  faq[3].answer = "more than before";
</cfscript>

<cfif NOT isNumeric(url.faq)>
  <cfset url.faq = 1>
<cfelse>
  <cfset url.faq = round(url.faq)>
  <cfif url.faq LT 1>
    <cfset url.faq = 1>
  <cfelseif url.faq GT arrayLen(faq)>
    <cfset url.faq = arrayLen(faq)>
  </cfif>
</cfif>

<!--- Content --->
<cfoutput>
  <strong>#faq[url.faq].question#</strong><br />
  Answer:&nbsp; #faq[url.faq].answer#<br /><br />
</cfoutput>

All Questions
<hr />
<cfloop from="1" to="#arrayLen(faq)#" index="iFAQ">
  <cfoutput>
    <strong>Q</strong>: <a href="?faq=#iFAQ#">#faq[iFAQ].question#</a><br />
  </cfoutput>
</cfloop>

We will skip the screenshot because there is no change to how the user sees the page. What is different is only some processing logic to prevent someone from messing with the stability of the page. While the stability of someone hacking a FAQ isn't a big deal, this will lay the foundation for protecting things such as e-commerce pages as your skills grow.

Oh, perhaps earlier you were thinking that we forgot the Boolean variable type. The conditional statements inside of these if statements actually evaluate to either true of false. These are the Boolean values. You will also find that you can use either a zero or nonzero number to represent a Boolean logical evaluation. Therefore, any number that evaluates to either zero or false has the same result. The other nonzero numbers and true, yes, and no are also matched Boolean conditions. You could just take the same code and assign it to a variable. Then, you could use the variable inside the if statement instead of evaluating the logic inside of the statement. Normally, just put it inside of the <cfIf> statement and you will do well:

<cfset myBoolean = NOT isNumeric(url.faq)>

First we check to make sure that the variable is in fact a number. I suggest you change the value in the address bar to text just to see that this doesn't break the page. You will find that it chooses to show the first item because the input in the URL variable is now invalid. This is done using the NOT logical condition. The NOT takes the result of the test and reverses it. You will also notice that if this is not the condition, then an alternate set of code is processed.

<cfif NOT isNumeric(url.faq)>

Next we will attempt to break it by entering in a negative number. As there are no items at that location in the array index, it would normally have broken the page. We have prevented that with our conditional logic by again resetting the value when any basic type of hack occurs.

Let's take one more look at the code with all the processing logic inside CFScript. If you use another platform and like script more, this may be your style. If it is not your style there is no problem. The beauty of the platform is that it is flexible. You can do it both ways.

<!--- Example: 1_21.cfm --->
<!--- Processing --->
<cfparam name="url.faq" default="1">

<cfscript>
  faq = arrayNew(1);
  faq[1] = structNew();
  faq[1].question = "What is the speed limit?";
  faq[1].answer = "55";	
  faq[2] = structNew();
  faq[2].question = "What is a car?";
  faq[2].answer = "Depends who you ask!";
  faq[3] = structNew();
  faq[3].question = "How much is gas?";
  faq[3].answer = "more than before";

if(! isNumeric(url.faq)){
  url.faq = 1;
} else {
  url.faq = round(url.faq);
  if(url.faq < 1){
    url.faq = 1;
  } else if(url.faq > arrayLen(faq)){
    url.faq = arrayLen(faq);
  }
}
</cfscript>
<!--- Content --->
<cfoutput>
  <strong>#faq[url.faq].question#</strong><br />
  Answer:&nbsp; #faq[url.faq].answer#<br /><br />
</cfoutput>

All Questions
<hr />
<cfloop from="1" to="#arrayLen(faq)#" index="iFAQ">
  <cfoutput>
    <strong>Q</strong>: <a href="?faq=#iFAQ#">#faq[iFAQ].question#</a><br />
  </cfoutput>
</cfloop>

We only show the top half of the code here because the content section of the code is identical to the previous example. You may note that you can use some more traditional script-style logic symbols when coding in script. With both of these in the book, it should help anyone to evaluate between syntax for either script or tag-based logic.