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

Understanding arrays


The array is an interesting variable construct. You can think of an array as a collection of commonly named variables with a numeric index. We will take the last code example and transfer it to arrays. Here we will change the backend in major ways, but the frontend will run completely the same from a user's point of view. If you look closer, you will be able to see that we are now passing a variable from page to page rather than the whole questions as before. Let's check out the code:

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

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

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

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

Rather than two lists at the top, we now have two array variable constructs. Before you start assigning variables to an array, you need to declare the variable as an array type. Arrays can be multi-dimensional. The truth is this is very rarely done, but you may find some use case now and then. We will focus on a single-dimensional array. If you forget to pass in the number of dimensions when you declare an array, you will get an error when that code runs. So, do remember to declare the number of dimensions.

Note

The maximum number of dimensions in ColdFusion is three. Remember that a large array in multi dimensions can consume huge amounts of computer memory. Think twice before using multi-dimensional arrays for this reason.

You will find a rich collection of array functions built into ColdFusion. You can use arrayDeleteAt() to remove an item in the middle of an array. There is also an arrayInsertAt() which does the opposite. There are three things that you need to keep in mind when using arrays:

  • Don't remove items in the middle of an array list without using the built-in functions. This could create a missing element and create an error when looping through the array collection.

  • Don't count on an item staying in the same indexed position. It may seem odd that we call the position an index and think the item can move. This is different because unlike the index in a book, arrays are dynamic.

  • The number of items in an array can change. It is best to use the arrayLen() function as we did in the example code in order to know the current length of an array.

Now we will perform a minor rewrite and run the same code as a multi-dimensional array. This is also called an array of structures. Each dimension of the array has structure. This allows for some unique layout of data within your application memory. We will add a CFDump at the end of the code, so the structure created can be presented for view:

<!--- Example: 1_19.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>

<!--- 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>
<cfdump var="#faq#">

Now we have the same basic information but this time our browser window shows the array of structures feeding the output:

We have only touched the surface of what you can do with structures, arrays, and loops. The masters of ColdFusion are still finding creative uses for them. They are pretty simple to master and very flexible to implement.

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