Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Coldfusion nested loops question

Posted on 2012-04-06
4
Medium Priority
?
377 Views
Last Modified: 2012-04-08
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_#LoopCount#"]>
        <cfset variables.teamname = form["sel_teamname_#LoopCount#"]>
        <!---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.schoolname] = GetTeams.team_name />
                </cfif>
             </cfloop>
</cfloop>
0
Comment
Question by:roger_v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 37819479
Hope this clarifies how the array of structures works.
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,team_name","Integer,VarChar")>
<cfloop from="1" to="5" index="tx">
<cfset tt=QueryAddRow(GetTeams)/>
<cfset tt=QuerySetCell(GetTeams,"team_id",tx)>
<cfset tt=QuerySetCell(GetTeams,"team_name","team_"&tx)>
</cfloop>

<cfset teamArray=ArrayNew(1)>
<cfset teamCnt=1>
<cfloop index="loopCount" from="1" to="#val(form.schoolcount)#">
      <cfset variables.schoolname = form["schoolname_#LoopCount#"]>
    <cfset variables.teamname = form["sel_teamname_#LoopCount#"]>
      <cfdump var="#variables.schoolname#"/>
      <cfdump var="#variables.teamname#"/>
    <!---begin check for teamnames--->
         <cfloop query="GetTeams">
          <cfif CompareNoCase(trim(GetTeams.team_name[GetTeams.currentrow]),trim(variables.teamname)) eq 0>
            <cfset tt=StructInsert(teamstruct,"schoolname",#GetTeams.team_name[GetTeams.currentrow]#)/>
                  <cfset teamArray[teamCnt] = teamStruct>
                  <cfset teamCnt=teamCnt+1>
         </cfif>
      </cfloop>
</cfloop>

<!--- Dump the teamArray --->
<cfdump var="#teamArray#"/>
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 37819515
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",tx)>
<cfset tt=QuerySetCell(GetTeams,"team_name","team_"&tx)>
</cfloop>

<cfset teamArray=ArrayNew(1)>
<cfset teamCnt=1>
<cfloop index="loopCount" from="1" to="#val(form.schoolcount)#">
      <cfset variables.schoolname = form["schoolname_#LoopCount#"]>
    <cfset variables.teamname = form["sel_teamname_#LoopCount#"]>
      <!---<cfdump var="#variables.schoolname#"/>
      <cfdump var="#variables.teamname#"/>--->
    <!---begin check for teamnames--->
         <cfloop query="GetTeams">
          <cfif CompareNoCase(trim(GetTeams.team_name[GetTeams.currentrow]),trim(variables.teamname)) eq 0>
                  <cfset teamStruct = structnew() />
            <!---this is the struct that is not being updated properly--->
            <cfset tt=StructInsert(teamstruct,"schoolname",#GetTeams.team_name[GetTeams.currentrow]#)/>
                  <cfset teamArray[teamCnt] = teamStruct>
                  <cfset teamCnt=teamCnt+1>
         </cfif>
      </cfloop>
</cfloop>

<!--- Dump the teamArray --->
<cfdump var="#teamArray#"/>
0
 
LVL 1

Author Comment

by:roger_v
ID: 37819779
@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?
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 2000 total points
ID: 37820288
What I am using is array of structures, as at the end of loop we have many elements.

Please read the article at sitepoint.

http://www.sitepoint.com/coldfusion-ii-structs/
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question