I find that I have to use the second method (assigning outerloop id to a
variable) to get nested query loops to work right. I've never understood
why, but that has worked for me.

> I am not 100% sure, but I think I know what is going on (Something similar
> has happened to me before, too).  In your third to last line, where you
> output "topmenu.id:leftmenu.id", the CFOUTPUT gets confused.  Rather than
> thinking that you are outputting each instance from the first CFLOOP, it
> thinks that you are generally referencing to the query "topmenu", and when
> you do that, it just returns the first pice of data in the resultset, which
> in this case, is "1".  Either of these solutions should fix the problem,
> just try them both, in case one doesn't:
>
> 1: (puts the outer-most CFLOOP as a CFOUTPUT)
> <cfquery name="topmenu" datasource="batu2">
>       select id
>       from categories
>       order by orderid
> </cfquery>
> <cfoutput query="topmenu">
>       #topmenu.id#::<br>
>       <cfquery name="leftmenu" datasource="batu2">
>               select id
>               from menu
>               where parent=#topmenu.id#
>       </cfquery>
>       <cfloop query="leftmenu">
>               #topmenu.id#:#leftmenu.id#<br>
>       </cfloop>
> </cfoutput>
>
> 2: (sets the topmenu.id it a new variable before using it)
> <cfquery name="topmenu" datasource="batu2">
>       select id
>       from categories
>       order by orderid
> </cfquery>
> <cfloop query="topmenu">
>       <cfset topmenuID = topmenu.id>
>       <cfoutput>#topmenuID#::<br></cfoutput>
>       <cfquery name="leftmenu" datasource="batu2">
>               select id
>               from menu
>               where parent=#topmenuID#
>       </cfquery>
>       <cfloop query="leftmenu">
>               <cfoutput>#topmenuID#:#leftmenu.id#<br></cfoutput>
>       </cfloop>
> </cfloop>
>
> Hope this helps
> -Brent
>
> -----Original Message-----
> From: xrussx [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, August 15, 2001 5:03 PM
> To: CF-Talk
> Subject: nested loops/queries
>
>
> I'm having a weird problem doing a nested loop and a nested query.  I
> have two queries.  The topmenu query returns ids 1,2,3 and 4.  The
> leftmenu query  returns ids 5,6,7,8,9 for topmenu.id=1 and 14,15,16 for
> topmenu.id=3.  The queries are right and return the right data.  For
> some reason when I'm trying to output it, it messes up the topmenu.id
> and thinks that it's 1 when it's supposed to be 3.  Below is the code
> and the output.  Am I doing something wrong?
>
>
> <cfquery name="topmenu" datasource="batu2">
>       select id
>       from categories
>       order by orderid
> </cfquery>
>
> <cfloop query="topmenu">
>       <cfoutput>#topmenu.id#::<br></cfoutput>
>       <cfquery name="leftmenu" datasource="batu2">
>               select id
>               from menu
>               where parent=#topmenu.id#
>       </cfquery>
>       <cfloop query="leftmenu">
>               <cfoutput>#topmenu.id#:#leftmenu.id#<br></cfoutput>
>       </cfloop>
> </cfloop>
>
>
>
>
> 1::
> 1:5
> 1:6
> 1:7
> 1:8
> 1:9
> 2::
> 3::
> 1:14
> 1:15
> 1:16
> 4::
>
> Russ
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to