More loopy looism
Here's a little thing I was forced to realise after a far too high profile site when slightly wrong (in a far too big way).
Looping a query inside a loop of another query changes the way you can refer to the outer-loops current row. Running the following code demonstrates this:
<cfscript>
qry_foo = QueryNew('foo');
QueryAddRow(qry_foo, 3);
for(i=1; i LTE 3; i=i+1){
QuerySetCell(qry_foo,'foo',"foo #i#",i);
}
qry_bar = QueryNew("bar");
QueryAddRow(qry_bar, 10);
for(i=1; i LTE 10; i=i+1){
QuerySetCell(qry_bar,'bar',"bar #i#",i);
}
</cfscript>
<cfoutput>
<ul>
<cfloop query="qry_foo">
<li>#qry_foo.foo#
<ul>
<cfloop query="qry_bar">
<li>#qry_foo.foo#</li>
</cfloop>
</ul>
</li>
</cfloop>
</ul>
</cfoutput>
The problem is that you cannot access the current row of the qry_foo query when inside the loop of qry_bar in this way. Instead, you would have to use:
qry_foo.foo[qry_foo.currentRow]Does anyone else find this bizarre?
Labels: ColdFusion

3 Comments:
Although it's on the mildly odd side, I rarely ever find myself looping in that fashion. I did it more back in '98 when I first started working with ColdFusion and didn't have a real great grasp of SQL (particularly outer joins and database normalization). But these days generally speaking if I'm looping over anything that resembles this I usually will have combined those queries and be using cfoutput with a group attribute, which has its own set of quirks.
By
info, At
20 December 2007 02:54
Man, I remember blogger having a better system for posting comments... what happened to commenting with OpenID? It required a password, but looks like it didn't actually authenticate anything -- it looks like it attempted to authenticate and when it failed just said "oh, use the first part of his email address to label the comment". Pheh! That's such a bad usability case I want to say "you get what you pay for", but if I were getting this service for free (blogger), I'd be tempted to ask for some kind of incentive to use it. Anyway, that's enough of my usability rant... :)
By
info, At
20 December 2007 02:58
lol @ "info". I always do things in SQl where it is possible; this loop was neccessary for the task at hand - but yeah, seems I've stumbled on an age old itch for the first time (it's the first time I've needed to loop in this way).
By
Dom, At
20 December 2007 10:18
Post a Comment
<< Home