Link to home
Start Free TrialLog in
Avatar of James Rodgers
James RodgersFlag for Canada

asked on

looping though array and assigning values to dynamicaly named variables

i have a form which is generated by the user inputing 3-5 project names resulting in 150 to 250 checkboxes (50 per project) tthat form works perfectly but when the user submits this form i needt to generate a table that contains values calculated upont the users selections from the checkbox form, i am trying to use a doulble loop to count the number of selections based upon the value of the selections(there are 10 different values in the 50 questions per project)

i was trying to do this to loop through the projects and the fields

<cfloop from="1" to="arrayLen(arrProjects)" index="prj">
      <cfloop from="1" to="arrayLen(arrFields)" index="fld">
<cfswitch expression="right(arrFields[fld],2)">
      <cfcase value="P#prj#">
            <cfswitch expression="evaluate(arrFields[fld])">
                  <cfcase value="AA">
                        <cfif not isDefined('P#prj#AA')>
                              <cfset P#prj#AA = 1>   <<crashes on this


<cfswitch expression="right(arrFields[fld],2)"> i us this to get the project id of the answer field name is skill2Px where x=1-5

<cfswitch expression="evaluate(arrFields[fld])"> gets the value of the fiield (AA, EE, FF, QQ, WW,OO,ZZ etc) and based upon the value add 1 to a count

the final product is supposed to look like this

         P1      P2      P      P4       P5    TTL
AA     3        2        5      5         4     19
EE     4        1         1      0        0      6


any help would be appreciated
SOLUTION
Avatar of danrosenthal
danrosenthal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Rodgers

ASKER

now i am getting this

CFCASE tag must have a constant value.


for this line

 <cfcase value="P#prj#">
Avatar of danrosenthal
danrosenthal

you probably want: <cfcase value="#evaluate('P'&prj)#">
<cfcase value = Evaluate("P#prj#")>
no neither suggestion worked, any ideas on a better way to acheive what i need?

i am really trying to turn a 5x50 2d arrayinto a 5x10 2d array by summig based upon the value...
any other methods you can suggest?
not really sure exactly what the goal is...
but I believe the first 3 lines should be:

<cfloop from="1" to="#LISTLEN(arrProjects)#" index="prj">
      <cfloop from="1" to="#LISTLEN(arrFields)#" index="fld">
            <cfif right(LISTGETAT(arrFields,fld),2) eq evaluate('P'&prj)>
that makes no sense.   how do you plan to add 50 rows together to make 10?
     
yeah confused !

what exactly are u looking for ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>how do you plan to add 50 rows together to make 10?
>>yeah confused !


the fifty rows only have ten possible values, 5 questions for each value,
the user reads each question and if it applied to the project then check it off when they have completed the form, they can answer one or all, i need to count the individual values by project and then sum the values by value, kind of a fake crosstab query, but from form input rather than a query
so that this

Form.SKILL1P1 AA
Form.SKILL1P2 EE
Form.SKILL1P3 FF
Form.SKILL1P4 AA
Form.SKILL1P5 EE
Form.SKILL2P1 AA
Form.SKILL3P2 EE
Form.SKILL4P3 EE
Form.SKILL5P4 EE
Form.SKILL6P5 ZZ
Form.SKILL7P4 ZZ
Form.SKILL8P3 ZZ
Form.SKILL9P2 ZZ
Form.SKILL10P1 FF
Form.SKILL11P2 WW
Form.SKILL12P3 AA
Form.SKILL13P4 WW
Form.SKILL14P5 FF

becomes

   Results Matrix    Project 1 Project 2 Project 3 Project 4 Project 5    total
AA                              2        0             3            5           1              11
WW                           3         1             2           2              2             10
EE                             2          3             3           3              3            14
FF                              0        0               2           4             4            10
ZZ                             1        1               1             1             1            5
QQ                            1         5               5             5            1            17
OO
 
 i can parse out the project id from the field names and get the values but i am having a hrd time getting the counts
procept,

this line is giving me an error

<cfset valueList = "AA,EE,FF,QQ,WW,OO,ZZ">

Invalid parser construct found on line 31 at position 18. ColdFusion was looking at the following text:

=
try using something else than valuelist it is a reserved word...make it value_list or listofvalues or anything ...valuelist is a function so CF is probably getting confused.
thanks ...

realized that after a while

but then i get this error
 <cfset p[prj][v] = ListValueCount(fieldList, v)>
The member "1" in dimension 1 of object "p" cannot be found. Please, modify the member name.


which i tried to fix with this
<cfset p['P'&prj][v] = ListValueCount(fieldList, v)>
but got the same error

The member "P1" in dimension 1 of object "p" cannot be found. Please, modify the member name.

changed procept's code to this

<cfset valuesList = "DD,GG,KK,NN,OO,RR,SS,TT,VV,ZZ">
<cfset P = structNew()>
<cfloop from="1" to="#arrayLen(arrProjects)#" index="prj">
   <cfset fieldList = ArrayToList(arrFields)>
   <cfloop list="#valuesList#" index="v">
        #PRJ# #V#
      <cfset structinsert(p ,'p'&#prj#&#v# ,ListValueCount(fieldList, v))>
        #P['p'&prj&v]# <br>
   </cfloop>
</cfloop>

and i get this
1 DD 0
1 GG 0
1 KK 0
1 NN 0
1 OO 0
1 RR 0
1 SS 0
1 TT 0
1 VV 0
1 ZZ 0
2 DD 0
2 GG 0
2 KK 0
etc.
 it seems to be working but it insnt adding the count
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
got it working with this...
a variation of Procept's code
<cfloop from="1" to="#arrayLen(arrProjects)#" index="prj">
<cfset valuesList ="P#prj#DD,P#prj#GG,P#prj#KK,P#prj#NN,P#prj#OO,P#prj#RR,P#prj#SS,P#prj#TT,P#prj#VV,P#prj#ZZ">
   <cfset fieldList = ArrayToList(arrFields)>
   <cfloop list="#valuesList#" index="v">
      <cfset "P#prj##v#" = ListValueCount(fieldList, v)>
   </cfloop>
</cfloop>

thanks all
i will try out your new code also, looks better than what i have