0

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 
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Geo
  • 3,160
  • 6
  • 41
  • 82
  • In order for us to help you, you're going to need to distill the code down to its bare essence. Leave only the stuff that's causing an issue; remove all the extra display logic. – ale Apr 13 '12 at 16:27
  • It'd also be helpful to see an example of what your data looks like. – ale Apr 13 '12 at 16:33
  • Also, if you take out the selects and dump the values of your variables you're using, do they look right? – ale Apr 13 '12 at 16:34
  • Yes, the variables look ok. I just need to loop through that table and for each day and get the `hour`. That's why i need the loop to go around 7 times and pull out the different times – Geo Apr 13 '12 at 16:39
  • By the by, you could probably save yourself a lot of heartache if you change day 1 to be Sunday (making Saturday day 7). This will match ColdFusion's internal workings and you can use functions like `dayOfWeekAsString()`. – ale Apr 13 '12 at 17:43

3 Answers3

1

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.

ale
  • 6,369
  • 7
  • 55
  • 65
  • can you make it a bit more clear? I am not familiar with this notation . Thanks – Geo Apr 13 '12 at 17:44
  • @Crematorio: Sure. Is that better? – ale Apr 13 '12 at 17:51
  • Yes much clearer. I don't have much experience with CF yet so thanks for explaining everything – Geo Apr 13 '12 at 18:43
  • im getting this error when I am trying to use your example: Element openHours8 is undefined in a Java object of type class coldfusion.filter.FormScope. Any suggestions? – Geo Apr 20 '12 at 17:09
  • 1
    That almost certainly means that openHours8 doesn't exist. Put a `...` around the line that's throwing the error. This also indicates that your loop is going too far. – ale Apr 20 '12 at 17:39
0

I think you want this, or something very similar:

<cfoutput>
<cfloop from="1" to="7" index="CountVar">        
     #openHours[CountVar]#<br>
</cfloop>
</cfoutput>
Evik James
  • 10,335
  • 18
  • 71
  • 122
  • I have tried that already. I have also tried `` but it did not work. It just gave me more info such us `openHours1[24]` – Geo Apr 13 '12 at 16:18
0

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.

Barry
  • 432
  • 5
  • 9
  • Thanks for your response. I am working on rewriting the code on this one because I used an earlier version of a similar code that I had that is why I am having so many issues. I will update this post when I am done. Thanks again – Geo May 01 '12 at 14:50