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

Introduction to lists and loops


Lists are stored inside string variables. You can also have a list variable stored inside a structure. Lists have a "separator" which is also known as a delimiter to divide the items so the server can evaluate the items. We are going to go back to our FAQ application concept and build on what we have learned. Let's look at the code for lists:

<!--- Example: 1_16.cfm --->
<!--- Processing --->
<cfparam name="url.speed" default="10">
<cfparam name="url.acceleration" default="0">

<cfscript>
  questions = "What is the speed limit?,What is a car?,How much is gas?";
</cfscript>

<!--- Content --->
<cfoutput>
  The second question is:<br />&nbsp;&nbsp;&nbsp; 
  #listGetAt(questions,2)#
</cfoutput>

The browser window now shows us the second question:

You can see that the list has three items in it. We have the content request the second item for display. The listGetAt() function is another one of those simple, powerful functions that makes ColdFusion so easy to program. You will find a number of wonderful list functions built into the language. We are going to mix lists and loops so you can see how things work together:

<!--- Example: 1_17.cfm --->
<!--- Processing --->
<cfparam name="url.question" default="What is the speed limit?">

<cfscript>
  questions = "What is the speed limit?,What is a car?,How much is gas?";
  answers = "55,Depends who you ask!,more than before";
  myQuestion = listContains(questions,url.question);listContains(questions,url.question);
</cfscript>

<!--- Content --->
<cfoutput>
  <strong>#listGetAt(questions,myQuestion)#</strong><br />
  Answer:&nbsp; #listGetAt(answers,myQuestion)#<br /><br />
</cfoutput>

All Questions
<hr />
<cfloop list="#questions#" index="iQuestion">
  <cfoutput>
    <strong>Q</strong>: <a href="?question=#iQuestion#">#iQuestion#</a><br />
  </cfoutput>
</cfloop>

Our browser now shows us the power of looping in action:

We made two lists this time, one list for questions called, of course, "questions" and another for answers called "answers". When you build pairs of lists like this, check twice that they have the same number of items in each list to prevent errors. This will keep us out of the debugging phase of development. If you look where we assigned the numeric value of myQuestion, you will see that we are able to match the question asked to the list. If there is an exact match, then the number of that item in the list is returned. You should also note that in the CFLoop list, the index variable contains the actual item stored in that position in the list.

Click on different questions and see how things work. You will be able to see the variables being passed in the address bar.

Note

ListContains() will work to find exact matches in a list. If you just want to find the first item in a list with a partial match, then use ListFind(). Both of these also have a NoCase version available.

We will continue to provide more information on loops as we get into arrays in the next segment! There are several types of loops and this is one of the more popular commands among ColdFusion developers.

You are coming along great and have nearly finished your first chapter. You may not realize it yet, but by now you have started to learn to think in ColdFusion. Don't expect to be a master or even understand the fine points of applying your knowledge. That knowledge will come with the chapters that follow. Right now, we are just building a foundation.

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