• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with Looping Form.fieldnames to get count

New Here ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

530

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

<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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

I wish.. but not in this case. You never know how rowdy these crazy hockey players will get, thus how many penalties will get submitted.

I know there are some javascript ways I can do it, but I will look at that later.

Thanks for all your help. I am making HUGE use of the FORM["text" & #v#] options you helped me with Sunday. What a world that has opened up!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2009 Nov 17, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 18, 2009 Nov 18, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2009 Nov 18, 2009

Copy link to clipboard

Copied

LATEST

> It has limited usefulness and has to be changed whenever something else on the form changes.

No. If done properly it is keyed off of a dynamic value. It is not a) hard-coded like the fieldnames method and b) in this scenario the fieldnames method is far more likely to require changes when fields are added.

> I think it's a bad idea.  Looping through formfields is reasonably simple

> once you get the hang of it.

Personally, I think storing a single number is much simpler than depending on a bunch of variable field names/strings. Especially when CONTAINS is used. String comparisons are notoriously error prone, and people often misunderstand how the operator works. Then run into problems when similar field names are added and the result is wrong or unexpected behavior. There is far less room for error in a single numeric comparison. Granted nothing is suitable for every case.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation