Skip to main content
Participating Frequently
March 25, 2008
Question

Loop Over Nested Structure

  • March 25, 2008
  • 1 reply
  • 1539 views
Hello,

Can someone please demonstrate how I would use a cfLoop to loop over and output the contents of this fictious nested structure?



Thank You!
This topic has been closed for replies.

1 reply

mletsonAuthor
Participating Frequently
March 25, 2008
I figured it out. If anyone has a better solution, please let me know.

<cfloop from="1" to="#structCount(student)#" index="x">

<cfset variables.studentID = "student00" & x>

<tr>
<td><cfoutput>#student[studentID]["firstName"]#</cfoutput></td>
<td><cfoutput>#student[studentID]["lastName"]#</cfoutput></td>
<td><cfoutput>#student[studentID]["phone"]#</cfoutput></td>
</tr>

</cfloop>
Inspiring
March 25, 2008
mletson wrote:
> I figured it out. If anyone has a better solution, please let me know.
>
> <cfloop from="1" to="#structCount(student)#" index="x">
>
> <cfset variables.studentID = "student00" & x>
>
> <tr>
> <td><cfoutput>#student[studentID]["firstName"]#</cfoutput></td>
> <td><cfoutput>#student[studentID]["lastName"]#</cfoutput></td>
> <td><cfoutput>#student[studentID]["phone"]#</cfoutput></td>
> </tr>
>
> </cfloop>
>

A slightly simpler version using the structure form of the <cfloop...>
tag. It also demonstrates both array notation, that you used, as well
as dot notation. Finally it puts a single <cfoutput...> block around
the entire loop. This can provide a small but accumulative performance
improvement.

<cfoutput>
<cfloop collection="#student#" item="aStudent">
<tr>
<td>#aStudent["firstName"]# OR #aStudent.firstName#</td>
<td>#aStudent["lastName"]# OR #aStudent.lastName#</td>
<td>#aStudent["phone"]# OR #aStudent.phone#</td>
</tr>
</cfloop>
</cfoutput>