Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with loop

Status
Not open for further replies.

acford

MIS
Nov 12, 2003
90
0
0
GB
Hi, I can get this to work and have no idea why. The nested loop always seems to read get_costumecat.CostumeCategoryID as the same even though the main loop knows that its changing on each pass.

Please Help!!!


<cfquery name="get_costumecat" datasource="#DATASOURCE#">
SELECT * FROM CostumeCategory
</cfquery>

<cfquery name="get_costumes" datasource="#DATASOURCE#">
SELECT * FROM Costume
</cfquery>


<cfloop query="get_costumecat">
<font size="4"><strong><cfoutput>#CostumeCategoryName#</cfoutput> <cfoutput>#CostumeCategoryID#</cfoutput></strong></font><br>
<cfloop query="get_costumes">
<cfif get_costumecat.CostumeCategoryID EQ get_costumes.CostumeCategory>
<cfoutput>#CostumeName#</cfoutput><BR>
</cfif>
</cfloop>
</cfloop>
 
Try this:
Code:
<cfoutput>
<cfloop query="get_costumecat">
[red]<cfset CurCat = CostumeCategoryID>[/red]
  <font size="4"><strong>
    #CostumeCategoryName#
    #CostumeCategoryID#
  </strong></font><br>

  <cfloop query="get_costumes">
    <cfif [red]CurCat[/red] EQ CostumeCategory>
      #CostumeName#<BR>
    </cfif>
  </cfloop>
</cfloop>
</cfoutput>
The reason it keeps referring to the first query result is because by using "get_costumecat.CostumeCategoryID" you are referring to the actual query results, not the results being output by the first loop. Also notice that I moved your output tags outside of the first loop. It's better to open your output, run through your loops, then close your output than it is to keep continuously opening and closing outputs inside of your loops.





Hope This Helps!

ECAR
ECAR Technologies, LLC

"My work is a game, a very serious game." - M.C. Escher
 
ECAR's post is very good and was the solution i've used up until i stopped using nested query loops. here is another solution.

working off of ECAR's post (because it's cleaner) you can do this.
Code:
<cfoutput>
<cfloop [COLOR=red]from = "1" to = "#get_costumecat.recordCount#" index = "c"[/color]>
  <font size="4"><strong>
    #[COLOR=purple]get_costumecat.[/color]CostumeCategoryName[COLOR=blue][c][/color]#
    #[COLOR=purple]get_costumecat.[/color]CostumeCategoryID[COLOR=blue][c][/color]#
  </strong></font><br>

  <cfloop query="get_costumes">
    <cfif [COLOR=blue]get_costumecat.CostumeCategoryID[c][/color] EQ [COLOR=green]get_costumes.[/color]CostumeCategory>
      #[COLOR=green]get_costumes.[/color]CostumeName#<BR>
    </cfif>
  </cfloop>
</cfloop>
</cfoutput>

color key:
purple: now that you are not actually looping over the query you must include the query name
green: when you loop over a query you don't need to include the query name because CF knows what you mean, however THIS IS VERY BAD PRACTICE AND YOU SHOULD ALWAYS PREFIX YOUR VARIABLE SCOPE. that includes "form." and "session." and "url." etc...
red: is the major change to the external loop. notice how we aren't actualy looping over the query anymore.
blue: the blue shows how the loop knows what value to pull from the external loop.


now with all that said, whenever you find yourself looping over a query within a query you're doing it very inefficiently. I would be half tempted to suggest this is the reason MM doesn't allow the relationship between the inner and outer loops. you should really write one query using a join and pull only the cats that you want, ordering by catID then product. once you have the query use nested cfoutputs using the "group" attribute.

example:

Code:
<cfoutput query = "myQuery" group = "catID">
<strong>#myQuery.catName#</strong><br>
  <cfoutput>
  &nbsp;&nbsp;#myQuery.productNameInCat# is a very good #myQuery.productType#<br>
  </cfoutput><hr>
</cfoutput>
will produces something like:
Masks
devil is a very good rubber mask
old witch is a very good latex mask
-------------------
Hoods
grim reaper is a very good hooded robe
little red riding hood is a very good fairy tail hood
little bo peep is a very good fairy tail hood

Beware of programmers who carry screwdrivers.
 
Help... This is the out put that I want and I understand the code to display that to the screen, but I'm not sure how to write the query.

In the above example, what would "My query" include?
 
Actually, after playing around, I see how to use. My problem was in the grouping, not the query


Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top