Geo
Geo

Reputation: 3200

Getting values from CFLOOP

I am trying to pull out values from a CFLOOP and dump them but I seem to be missing something.I need to extract openHours from the first loop and openMinutes from the second and put them in variables that will then run a query for submitting the values in the database.

This is my struct when I dump out #form#. I need to get the variable form.openHours1 the problem is that openHours gets its number by #CountVar# so basically i need to dump out something like #form.openHours[CountVar]#

struct  
FIELDNAMES   POSTITNOW,OPENHOURS1,OPENHOURS2,OPENHOURS3,OPENHOURS4,OPENHOURS5,OPENHOURS6,OPENHOURS7
OPENHOURS1   13
OPENHOURS2   13
OPENHOURS3   12
OPENHOURS4   0
OPENHOURS5   0
OPENHOURS6   0
OPENHOURS7   0
POSTITNOW    YES 

Upvotes: 0

Views: 1226

Answers (3)

Barry
Barry

Reputation: 432

Sorry, this is a little murky to me, but that's never stopped me from jumping in. Are you going to have an equal number of openhours and openminutes? Can you just loop over form.fieldnames? As it stands now, you have fields named openhours1-N, it sounds like openminutes1-N is yet to be added. It seems that you could loop over fieldnames, if the field starts with openhours you get the number off the end and then you can easily create the corresponding openminutes field. As Al said (much) earlier, you would then most likely use array-notation to get the values out of the form structure.

Another thought is that form field names don't have to be unique. If you had multiple occurrences of "openhours", ColdFusion would turn that into a list for you, then you could just loop over that list.

Upvotes: 0

ale
ale

Reputation: 6430

Rather than #form.openHours[CountVar]# what you want is:

form["openHours" & CountVar]

As a scope, FORM is also a struct and you can use array notation to get at the values.

This is key for working with dynamic form field names.

To clarify:

form.openHours7

is equivalent to

form["openHours7"]

The first is generally known as dot-notation, the second as array-notation (since it resembles how you refer to array elements.

Since the value in the bracket is a string, you can replace it with a variable.

<cfset fieldToUse = "openHours7">
<cfoutput>#form[fieldToUse]#</cfoutput>

Or, as I opened with, a combination of a literal string and a variable.

You can't really do that with dot-notation. (At least not without using evaluate(), which is generally not recommended.)

The documentation has lots of information on how to work with structures, including the different notation methods.

Upvotes: 1

Evik James
Evik James

Reputation: 10503

I think you want this, or something very similar:

<cfoutput>
<cfloop from="1" to="7" index="CountVar">        
     #openHours[CountVar]#<br>
</cfloop>
</cfoutput>

Upvotes: 0

Related Questions