This post is a follow-up to a previous post. If you don’t think you need to read it or just don’t feel like it, I’ll just dump the code and a snapshot of how our struct that we’ll be looking at is structured. However, if you are interested, you can check it out at Creating a Struct by Looping. Anyway, here’s the code and snapshot from the other post.
<cfset VARIABLES.loops = 5>
<!— CREATE A STRUCT —>
<cfset VARIABLES.data = structnew()>
<!— SET A DATA INTO THE STRUCT. THIS REPRESENTS HOW TO HARDCODE DATA INTO THE STRUCT —>
<cfset VARIABLES.data.working = true>
<!— RUN OUR LOOP —>
<cfloop from=”1″ to=”#VARIABLES.loops#” index=”VARIABLES.i”>
<!— SET DATA DYNAMICALLY INTO THE STRUCT —>
<cfset “VARIABLES.data.loops.loop#VARIABLES.i#” = “loop_data_” & VARIABLES.i>
</cfloop>
<!— DUMP OUR STRUCT —>
<cfdump var=”#VARIABLES.data#”><br /><br /><br />

What our struct looks like.
So what we’re going to do is loop over the ‘loops’ portion of our struct and output each of the 5 loop labels data. So we are wanting to show the data from our struct like:
OUTPUT OUR LOOPED DATA IN OUR STRUCT
DATA 1: loop_data_1
DATA 2: loop_data_2
DATA 3: loop_data_3
DATA 4: loop_data_4
DATA 5: loop_data_5
The key is that we want to loop over the struct holding this data and output it dynamically. That way if it is 5 loops or 200 loops the data will always output the same and account the for the varying amounts of data. Let’s look at the code first.
<!— GET THE LENGTH OF OUR LOOPS STRUCT —>
<cfset VARIABLES.structLength = structcount(VARIABLES.data.loops)>
<!— LOOP THROUGH OUR STRUCT AND OUTPUT THE VALUES —>
<cfloop from=”1″ to=”#VARIABLES.structLength#” index=”VARIABLES.i”>
<!— SET THE NAME OF THE LABEL OF EACH OF SET OF DATA IN THE LOOP —>
<cfset VARIABLES.loopdata = “loop” & VARIABLES.i>
<!— GET THE DATA WE SET FOR THAT LOOP —>
<cfoutput>
<strong>DATA #VARIABLES.i#</strong>: #VARIABLES.data.loops[VARIABLES.loopdata]#<br />
</cfoutput>
</cfloop>
First we need to get how many sets of data is in our struct. Remember that the struct that I’m referring to here is the struct inside of the larger struct. You can get the amount of data in a struct by using the handy ’structcount()’ method. Just pass in the struct you want to get the count of. Afterwards, we can loop from 1 to however many sets of data is stored in our struct. Simple enough so far.
Once inside the loop we need to grab the first set of data in the struct we are looping over. This is the data with a label of ‘loop1′. First we set a var that will update with each loop to create the new label. So loop 1 will be set to ‘loop1′, second loop ‘loop2′, and so on. Once we have this set we can get our data by using brackets as opposed to just continuing the dot notation. For example we could get the value of ‘loop1′ by hardcoding the following: VARIABLES.data.loops.loop1. But that is just going to output the first set of data 5 times. In order to pass a dynamic value into our struct path that points to our value, we need to use the brackets. Using brackets, we can pass in our var that sets its value dynamically with each loop, which will output each of the 5 sets of data in our struct.
[...] pukkared Web Design & Development Adding DOM elements with jQuery Dynamically displaying struct data with a loop [...]
[...] Creating a Struct by Looping, and review how to output struct data dynamically with a loop with Dynamically Displaying Struct Data with a Loop. We’re setting data into our struct using our counter number which represents the item we [...]