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

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.