Reputation: 143
Not sure if this is possible or not. What I am trying to do is build an output string via queries. I am concatenating the output "name" and appending the "value" to the end. Then outputting the string. I don't think this is possible. But I am looking for any alternatives.
So this is what I have:
qry1
is the main query. qry2
gets the value to append to the end of the string.
So the value of variable test
would look like this: "variables.qry1.100"
Which would make sense to qry1
as this is part of the query object. So then this string would return a correct value from the database as there is a subquery called 100
<cfoutput>
<cfloop query="variables.qry2">
<cfset test = variables.qry1. & variables.qry2.#valueID#>
<td>#test#</td>
</cfloop>
</cfoutput>
Many thanks.
JC
Upvotes: 3
Views: 3158
Reputation: 246
Adam has the correct solution. Here is a modified version of your original code that does what I think you are trying to do.
<cfoutput query="variables.qry1">
<tr>
<cfloop query="variables.qry2">
<cfset test = variables.qry1[variables.qry2.valueID][variables.qry1.currentrow]>
<td>#test#</td>
</cfloop>
</tr>
</cfoutput>
Upvotes: 0
Reputation: 426
What your trying to do is possible, but you need to build a variable name first.
Instead of
<cfset test = variables.qry1. & variables.qry2.#valueID#>
Try
<cfset test = "variables.qry1.#variables.qry2.valueID#">
Test will then be variables.qry1.[valueID value]
. Note that [valueID value] is what is getting returned from the query, so the actual value inside the variable.
Then to display the value of variables.qry1.[valueID value]
.
#evaluate(test)#
UPDATE As stated by Adam Cameron's answer. You should really try to avoid the evaluate()
function, it's quite a performance hit and not considered good practice. Instead rather use the following code (This is copied from Adam Cameron's answer)
#variables.qry1[variables.qry2.valueID][1]#
NOTE: Go look at Adam Cameron's answer for a better description of whats going on.
Upvotes: 4
Reputation: 29870
So basically - given your example - you have a column in qry1
called 100
(etc, perhaps also 200
, 300
etc), and the values 100, 200, 300 etc are row values in the valueID
column of qry2
? And qry1
is a single-row query? Is that right?
If you have the name of the column in a dynamic string, you use this syntax:
queryName[columnName][rowNumber]
Where queryName
is the variable that is the query, columnName
is the string holding the column name, and rowNumber
is the row number (either an actual number, or a variable containing one).
So using your example variables, you code would be:
<td>#variables.qry1[variables.qry2.valueID][1]#</td>
There is no need to use evaluate()
to do this, and it is not a good solution here. There is very seldom a need to use evaluate()
in CFML, since the days of CF5.
I have no idea of the background of how your data structures came to be the way they are, but if you are needing to write the sort of code you are suggesting you need to... I'd be looking long and hard at how you're going about things.
Upvotes: 13