roger v
asked on
Coldfusion nested loops question
Hi,
I have a CF9 page where I have 2 loops - an outer loop that loops over form variables and an inner query loop that checks to see if there is match in a form variable from the outer loop in the inner query loop. If there is a match, then I need to populate a structure with a key/value pair - a form variable value from the outer loop is key and the query's resultset value is the value.
I've tried to do this but it's not giving me the desired result. Here is the code that I have:
<cfset teamStruct = structnew() />
<cfloop index="loopCount" from="1" to="#val(form.schoolcount) #">
<cfset variables.schoolname = form["schoolname_#LoopCoun t#"]>
<cfset variables.teamname = form["sel_teamname_#LoopCo unt#"]>
<!---begin check for teamnames--->
<cfloop query="GetTeams">
<cfif GetTeams.team_name is variables.teamname>
<!---this is the struct that is not being updated properly--->
<cfset teamStruct[variables.schoo lname] = GetTeams.team_name />
</cfif>
</cfloop>
</cfloop>
I have a CF9 page where I have 2 loops - an outer loop that loops over form variables and an inner query loop that checks to see if there is match in a form variable from the outer loop in the inner query loop. If there is a match, then I need to populate a structure with a key/value pair - a form variable value from the outer loop is key and the query's resultset value is the value.
I've tried to do this but it's not giving me the desired result. Here is the code that I have:
<cfset teamStruct = structnew() />
<cfloop index="loopCount" from="1" to="#val(form.schoolcount)
<cfset variables.schoolname = form["schoolname_#LoopCoun
<cfset variables.teamname = form["sel_teamname_#LoopCo
<!---begin check for teamnames--->
<cfloop query="GetTeams">
<cfif GetTeams.team_name is variables.teamname>
<!---this is the struct that is not being updated properly--->
<cfset teamStruct[variables.schoo
</cfif>
</cfloop>
</cfloop>
Use this one. I cleaned up few things
<cfif not isdefined("form.submit")>
<cfform name="f1">
<br/><cfinput type="hidden" name="schoolcount" value="3"/>
<br/><cfinput type="text" name="schoolname_1" value="school_1"/>
<cfinput type="text" name="sel_teamname_1" value="team_1"/>
<br/><cfinput type="text" name="schoolname_2" value="school_2"/>
<cfinput type="text" name="sel_teamname_2" value="team_2"/>
<br/><cfinput type="text" name="schoolname_3" value="school_3"/>
<cfinput type="text" name="sel_teamname_3" value="team_3"/>
<br/><cfinput type="submit" name="submit" value="Submit">
</cfform>
<cfexit>
</cfif>
<cfset GetTeams=QueryNew("team_id ,team_name ","Integer ,VarChar") >
<cfloop from="1" to="5" index="tx">
<cfset tt=QueryAddRow(GetTeams)/>
<cfset tt=QuerySetCell(GetTeams," team_id",t x)>
<cfset tt=QuerySetCell(GetTeams," team_name" ,"team_"&t x)>
</cfloop>
<cfset teamArray=ArrayNew(1)>
<cfset teamCnt=1>
<cfloop index="loopCount" from="1" to="#val(form.schoolcount) #">
<cfset variables.schoolname = form["schoolname_#LoopCoun t#"]>
<cfset variables.teamname = form["sel_teamname_#LoopCo unt#"]>
<!---<cfdump var="#variables.schoolname #"/>
<cfdump var="#variables.teamname#" />--->
<!---begin check for teamnames--->
<cfloop query="GetTeams">
<cfif CompareNoCase(trim(GetTeam s.team_nam e[GetTeams .currentro w]),trim(v ariables.t eamname)) eq 0>
<cfset teamStruct = structnew() />
<!---this is the struct that is not being updated properly--->
<cfset tt=StructInsert(teamstruct ,"schoolna me",#GetTe ams.team_n ame[GetTea ms.current row]#)/>
<cfset teamArray[teamCnt] = teamStruct>
<cfset teamCnt=teamCnt+1>
</cfif>
</cfloop>
</cfloop>
<!--- Dump the teamArray --->
<cfdump var="#teamArray#"/>
<cfif not isdefined("form.submit")>
<cfform name="f1">
<br/><cfinput type="hidden" name="schoolcount" value="3"/>
<br/><cfinput type="text" name="schoolname_1" value="school_1"/>
<cfinput type="text" name="sel_teamname_1" value="team_1"/>
<br/><cfinput type="text" name="schoolname_2" value="school_2"/>
<cfinput type="text" name="sel_teamname_2" value="team_2"/>
<br/><cfinput type="text" name="schoolname_3" value="school_3"/>
<cfinput type="text" name="sel_teamname_3" value="team_3"/>
<br/><cfinput type="submit" name="submit" value="Submit">
</cfform>
<cfexit>
</cfif>
<cfset GetTeams=QueryNew("team_id
<cfloop from="1" to="5" index="tx">
<cfset tt=QueryAddRow(GetTeams)/>
<cfset tt=QuerySetCell(GetTeams,"
<cfset tt=QuerySetCell(GetTeams,"
</cfloop>
<cfset teamArray=ArrayNew(1)>
<cfset teamCnt=1>
<cfloop index="loopCount" from="1" to="#val(form.schoolcount)
<cfset variables.schoolname = form["schoolname_#LoopCoun
<cfset variables.teamname = form["sel_teamname_#LoopCo
<!---<cfdump var="#variables.schoolname
<cfdump var="#variables.teamname#"
<!---begin check for teamnames--->
<cfloop query="GetTeams">
<cfif CompareNoCase(trim(GetTeam
<cfset teamStruct = structnew() />
<!---this is the struct that is not being updated properly--->
<cfset tt=StructInsert(teamstruct
<cfset teamArray[teamCnt] = teamStruct>
<cfset teamCnt=teamCnt+1>
</cfif>
</cfloop>
</cfloop>
<!--- Dump the teamArray --->
<cfdump var="#teamArray#"/>
ASKER
@pravinasar:
Instead of a struct you used an array. Why?
Also, I'm more familiar/comfortable with the whole key/value concept of structs. Is it possible to use struct instead of array?
Instead of a struct you used an array. Why?
Also, I'm more familiar/comfortable with the whole key/value concept of structs. Is it possible to use struct instead of array?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is complete example. You may also notice use of QueryNew() feature of CF9
<cfform name="f1">
<br/><cfinput type="hidden" name="schoolcount" value="3"/>
<br/><cfinput type="text" name="schoolname_1" value="school_1"/>
<cfinput type="text" name="sel_teamname_1" value="team_1"/>
<br/><cfinput type="text" name="schoolname_2" value="school_2"/>
<cfinput type="text" name="sel_teamname_2" value="team_2"/>
<br/><cfinput type="text" name="schoolname_3" value="school_3"/>
<cfinput type="text" name="sel_teamname_3" value="team_3"/>
<br/><cfinput type="submit" name="submit" value="Submit">
</cfform>
<cfexit>
</cfif>
<cfset GetTeams=QueryNew("team_id
<cfloop from="1" to="5" index="tx">
<cfset tt=QueryAddRow(GetTeams)/>
<cfset tt=QuerySetCell(GetTeams,"
<cfset tt=QuerySetCell(GetTeams,"
</cfloop>
<cfset teamArray=ArrayNew(1)>
<cfset teamCnt=1>
<cfloop index="loopCount" from="1" to="#val(form.schoolcount)
<cfset variables.schoolname = form["schoolname_#LoopCoun
<cfset variables.teamname = form["sel_teamname_#LoopCo
<cfdump var="#variables.schoolname
<cfdump var="#variables.teamname#"
<!---begin check for teamnames--->
<cfloop query="GetTeams">
<cfif CompareNoCase(trim(GetTeam
<cfset tt=StructInsert(teamstruct
<cfset teamArray[teamCnt] = teamStruct>
<cfset teamCnt=teamCnt+1>
</cfif>
</cfloop>
</cfloop>
<!--- Dump the teamArray --->
<cfdump var="#teamArray#"/>