Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ColdFusion 9 Developer Tutorial

You're reading from   ColdFusion 9 Developer Tutorial Create robust professional web applications with ColdFusion

Arrow left icon
Product type Paperback
Published in Jul 2010
Publisher Packt
ISBN-13 9781849690249
Length 388 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
John Farrar John Farrar
Author Profile Icon John Farrar
John Farrar
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

ColdFusion 9 Developer Tutorial
Credits
About the Author
About the Reviewers
1. Preface
1. Web Pages—Static to Dynamic 2. Basic CFCs and Database Interaction FREE CHAPTER 3. Power CFCs and Web Forms 4. ORM Database Interaction 5. Application, Session, and Request Scope 6. Authentication and Permissions 7. CFScript 8. CF AJAX User Interface 9. CF AJAX Forms 10. CF AJAX Programming 11. Introduction to Custom Tags 12. ColdFusion Powered Views 13. Control Logic Processing 14. Guide to Unit Testing Beyond this Book Tools and Resources 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image