- ColdFusion 9 Developer Tutorial
- John Farrar
- 489字
- 2021-08-05 16:16:35
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 /> #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: #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.
- ColdFusion 9 Developer Tutorial
- Joomla! 1.5 Site Blueprints
- PS職場達人煉成記:人人都能學會的Photoshop辦公設計技巧
- Sencha Touch Cookbook, Second Edition
- Photoshop CS6 互聯網應用設計教程
- 移動App測試的22條軍規
- 攝影照片修飾完全自學手冊
- Mastering phpMyAdmin 3.3.x for Effective MySQL Management
- Moodle JavaScript Cookbook
- Premiere視頻編輯項目教程:Premiere Pro 2020(微課版)
- UG NX 9中文版從入門到精通
- 中文版Photoshop 2022基礎教程
- 中文版3ds Max/VRay效果圖制作完全自學教程(實例培訓教材版)
- Origin 2022科學繪圖與數據分析(高級應用篇)
- Illustrator CC 2018 基礎與實戰教程(全彩版)