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

Count the number of values selected in a form

Guest
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

Hello

I need to count the number of values selected in a form.

Here is my form dump:

countForm.png

I want to count the values circled in red.

So the number I want in this case is 5.

Any suggestions?

Views

8.0K

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

correct answers 1 Correct answer

Guide , Nov 06, 2013 Nov 06, 2013

Aegis,

Yeah, good point.  Here is a minor tweak:

<cfset total = 0>

<cfloop collection="#FORM#" item="field">

     <cfif field NOT IS "fieldnames">

       <cfset total += ListLen( FORM[ field ] )>

     </cfif>

</cfloop>

<cfoutput>

     <p>Total: #total#</p>

</cfoutput>

-Carl V.

Votes

Translate

Translate
Advocate ,
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

pjunkie wrote:

I need to count the number of values selected in a form.

I want to count the values circled in red.

So the number I want in this case is 5.

5? I only see 3 circles.

Try this:

nCount = 0;

For(sFormVar in Form) {

   If (Form[sFormVar] != "" )

      nCount++;

}

WriteDump(var=nCount, label="Number of values provided");

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
Enthusiast ,
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

<cfset total = 0 />

<cfloop collection="#FORM#" item="field">

     <cfif FORM[ field ] neq ''><cfset total += 1 /></cfif>

</cfloop>

<p>Total: #total#</p>

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
Guide ,
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

You get five if you count each item in the lists in the first and third form fields (ids 12053 and 12056) individually, correct?  So you need to loop over the collection and use ListLen() on the fields to count the items in each field (assuming that commas are always used to separate list items).  So this should work:

<cfset total = 0>

<cfloop collection="#FORM#" item="field">

     <cfset total += ListLen( FORM[ field ] )>

</cfloop>

<cfoutput>

     <p>Total: #total#</p>

</cfoutput>

-Carl V.

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
Enthusiast ,
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

Of course, doesn't CF throw in some extra fields into the FORM scope (FIELDNAMES) etc?  I would assume we would have to factor that one out of the results.

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
Guide ,
Nov 06, 2013 Nov 06, 2013

Copy link to clipboard

Copied

LATEST

Aegis,

Yeah, good point.  Here is a minor tweak:

<cfset total = 0>

<cfloop collection="#FORM#" item="field">

     <cfif field NOT IS "fieldnames">

       <cfset total += ListLen( FORM[ field ] )>

     </cfif>

</cfloop>

<cfoutput>

     <p>Total: #total#</p>

</cfoutput>

-Carl V.

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