Copy link to clipboard
Copied
I'm currently working with a large amount of data in a query, where I am looping through the query and working with each row of the query individually. I'm using:
<cfloop query="qryData">
#data1#
#data2#
etc...
</cfloop>
However, sometimes I will need to be able to refer to data in the previous row of the query, and compare it to data in the current row of the loop. Is this possible to do? Something like:
<cfloop query="qryData">
#data1#
#qryData[i-1].data2#
etc....
</cfloop>
Clearly, that's not the correct syntax, but I'm unsure of what is.
Any information would be greatly appreciated.
Thanks,
Copy link to clipboard
Copied
CFLOOP QUERY is one of the handy little CF shortcuts, looks like you'll have to go manual
<cfloop from="1" to="#myquery.recordcount#" index="i">
<cfoutput>#myquery.mycolumn# #myquery.mycolumn[i-1]#</cfoutput>
</cfloop>
Obviously you'll need to put in some logic for the first row to stop it referencing row 0, which won't exist.
Hth.
Copy link to clipboard
Copied
Actually, I think I just found a way to do it with the "cfloop query".
<cfloop query="qryData">
#data1#
#qryData.data2[currentRow + 1]#
</cfloop>
The #currentRow# variable is very useful here. You can add any integer to it to go up and down the rows in the query.
I guess I answered this thread for myself!
But thank you very much for the input anyways!
Copy link to clipboard
Copied
You need some CFIFs in there to test for current row = 1 when looking at previous rows, or currentrow = recordcount when looking at following rows, else you'll get some errors.
-reed
Copy link to clipboard
Copied
wcx08 wrote:
<cfloop query="qryData">#data1#
#qryData[i-1].data2#
etc....
</cfloop>
Clearly, that's not the correct syntax, but I'm unsure of what is.
But it was pretty close:
<cfloop query="qryData">
#data1#
#qryData.data2[qryData.currentRow - 1]#
etc....
</cfloop>
Copy link to clipboard
Copied
Hmm..Your way has a slightly different syntax than mine. Any specifics I should know about?
Thanks!
Copy link to clipboard
Copied
wcx08 wrote:
Hmm..Your way has a slightly different syntax than mine. Any specifics I should know about?
Thanks!