views:

2009

answers:

3

I have a set of column names in a table - e.g. foo1, foo2, foo3, foo4. I want to refer to these column names dynamically through a loop:

<cfloop index="i" from="1" to="4">
  <cfset foo = Evaluate("query.foo" & i)>
</cfloop>

The above doesn't work - ColdFusion throws a "variable not defined" error, even though query.foo1 is a valid reference to the query results. How else can I do this?

+1  A: 

You are very close. Try this:

<cfset query.foo1 = "foo val 1">
<cfset query.foo2 = "foo val 2">
<cfset query.foo3 = "foo val 3">
<cfset query.foo4 = "foo val 4">

<cfloop index="i" from="1" to="4">
        <cfset foo = Evaluate("query.foo#i#")>
        <cfoutput>#foo#<br></cfoutput>
</cfloop>
Mike Jr
I know Evaluate() has been used traditionally (i.e. up to CF5) for this kind of stuff, but since CF6 it is unnecessary and generally not recommendable. Even Macromedia/Adobe discourage its use: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000946.htm
Tomalak
Awesome, I didn't realize this. Thanks.
Mike Jr
+15  A: 

Don't use Evaluate() for things like that! It's slow and should be avoided.

<cfloop index="i" from="1" to="4">
  <cfset foo = query["foo" & i][query.CurrentRow]>
</cfloop>

Or, if you like:

<cfloop index="i" from="1" to="4">
  <cfset foo = query["foo#i#"][query.CurrentRow]>
</cfloop>

Evaluate() is for evaluating bits of code. Don't use it for things that can be solved more elegantly in language-integrated, more appropriate ways.

EDIT:

When accessing Query objects with the "angle bracket"-syntax, you must append the (1-based) row number index (query["foo#i#"][RowNum]). When using the traditional "dot"-syntax (query.foo1), the current row is implicit.

To access the current row explicitly, use the QueryObject.CurrentRow property. But it could be any positive integer up to QueryObject.RecordCount. A range check is advised for anything other than CurrentRow.

This opens an interesting field: You can start to use query objects with "random access". Previously (before CFMX) all you could do was iterate them from start to end, pulling out the things that you look for. Now it's like a nested struct/array data structure that you can use in different ways.

Tomalak
Unfortunately, neither of those work - ColdFusion tells me "the value foo1 cannot be converted to a number."
shifuimam
Oh, I see. Wait, there's a little catch about how to address query rows with the angle bracket syntax.
Tomalak
I'm not seeing that in the documentation you linked to - any suggestions?
shifuimam
The link works for me. The CF documentation traditonally loads a bit slow, and I think you need JavaScript on.
Tomalak
Oddly, I'm still getting the "cannot be converted to a number error", even after adding the [query.currentrow] syntax.
shifuimam
I've tested the above and it works as advertised. The error you get must be somewhere else.
Tomalak
It's definitely happening with that line of code. The field in question is a number, the data type is set to integer. If I take out that line of code, the page renders perfectly fine.
shifuimam
Anything I use with the queryname[fieldname][rownumber] syntax is trying to convert it to a number...
shifuimam
Then please post the portion that does not work for you, and a little context. I have no idea what's wrong, but the above code definitely works for CFMX 6.0 and higher.
Tomalak
shifuimam
And your CF version is?
Tomalak
I'm on CF8 and MSSQL 2k5. The result set is coming from a pretty generic SQL query called to in a CFC - e.g. "SELECT TaskID, Task, Max1, Max2, Max3, Max4 FROM Tasks ORDER BY Task"
shifuimam
Okay, I tested mine on CF7.1, which should produce equal results for this. Does a static <cfset foo = task["max1"][task.CurrentRow]> work for you?
Tomalak
For some bizarro reason, changing the name of the query results from "Task" to anything else (e.g. "Tasks" or "Tsk" makes it work - I didn't think "Task" was a reserved word in CF...)
shifuimam
I don't, either. But I assume you are seeing an odd scoping issue. I think the implicitly-scoped variable name "task" is matched against a higher-ordered scope, found there, and used without referring to your query result anymore.
Tomalak
It does seem as though everything is working properly now - thanks so much for your help!!
shifuimam
You're welcome. Glad we got it resolved. :-)
Tomalak
A: 

Hi, how can we insert the variable "foo" in an access database ?

thanks

chaton
"foo" is just a nondescript variable name commonly used for throwaway variables, objects, classes, etc. I have no idea how to use ColdFusion with Access databases.
shifuimam