Skip to main content
November 18, 2009
Question

Help with Looping Form.fieldnames to get count

  • November 18, 2009
  • 2 replies
  • 587 views

Hi all,

I have searched the forum for the answer for this, and have found similar posts, but not quite what I am looking for.

I have a form that submits values likes this: (All dynamically generated)

APEN_INFRACTION_10
APEN_INFRACTION_20
APEN_INFRACTION_30
APEN_INFRACTION_40
APEN_INFRACTION_50
APEN_INFRACTION_TYPE_10
APEN_INFRACTION_TYPE_20
APEN_INFRACTION_TYPE_30
APEN_INFRACTION_TYPE_40
APEN_INFRACTION_TYPE_50
APEN_PER_11
APEN_PER_21
APEN_PER_32
APEN_PER_40
APEN_PER_50
APEN_PLAYER_11
APEN_PLAYER_21
APEN_PLAYER_31
APEN_PLAYER_41
APEN_PLAYER_51

The problem I am having is that when I am looping to set a value, the count moves beyond the form value that exists in the form.field. Obvioulsy it is looping all form fields, so the count of fields is greater than the form variable I am looking to cfset to something else.

Example, if I loop this with the form values above:

<cfset acount=0>
<cfloop list="#form.fieldnames#" index="apen">
<cfset acount=#acount#+1>   
<cfif isDefined(FORM["APEN_PER_" & #acount#]) AND FORM["APEN_PER_" & #acount#] NEQ ''>
Boo
</cfif>

The loop moves past  "APEN_PER_5" and goes to "APEN_PER_6" which throws an error because it doesn't exist.

I would love to just loop the form field as a list, but for obvious reasons, I can't (or can I?).

Any Ideas how to get the individual column count from the form field in this scenario?

Thanks all,

Greg

This topic has been closed for replies.

2 replies

November 18, 2009

Answered my own question with this one.

Just looped the field names and if it contained certain text strings, increased a counter.

<cfset acount=0>
<cfloop list="#form.fieldnames#" index="apen">
    <cfif #apen# CONTAINS "APEN_PER">
    <cfset acount=#acount#+1>
    </cfif>
</cfloop>
<cfoutput>#acount#</cfoutput>

Thanks,

Greg

Inspiring
November 18, 2009

Is there a reason you cannot just store the number of fields in the first place? Contains should be used with caution, as similar field names can lead to a false positive.

<cfif #apen# CONTAINS "APEN_PER">

<cfset acount=#acount#+1>

BTW: Remove the extra # signs. They are not needed

Inspiring
November 18, 2009

Regarding:  Is there a reason you cannot just store the number of fields in the first place?

I think it's a bad idea.  It has limited usefulness and has to be changed whenever something else on the form changes.  Looping through formfields is reasonably simple once you get the hang of it.

Inspiring
November 18, 2009

<cfloop list="#form.fieldnames#" index="apen">

It is much easier if you store the total number of fields in a hidden form field. Then use that value in a from/to loop

...

</cfloop

November 18, 2009

cfsearching,

The problem is that I could have 60 form rows heading my way, or 3. With what you helped me with on Sunday, I knew how many records I needed to process, in this case I don't.

Thanks!

Greg

Inspiring
November 18, 2009
I knew how many records I needed to process, in this case I don't.

Oh, okay. I thought you had control over the input form.