Search code examples
dynamiccoldfusioncfoutput

How can I access the query for my cfoutput dynamically, based on a query name variable?


I have a struct that contains 2 queries. I have a variable with the "key" of one of the queries, and I want to output the query dynamically using that variable. My basic code:

<cfquery name="myQueries.names" ... >...</cfquery>
<cfquery name="myQueries.places" ... >...</cfquery>

<cfset queryName = "places" />

<cfoutput query="myQueries[queryName]">
...
</cfoutput>

This gives me the error Attribute validation error for tag cfoutput.

The cfoutput "query" attribute doesn't seem to support bracket notation. How can I access the query from the cfoutput?


Solution

  • The query attribute of cfoutput requires a valid variable name, so you can set an intermediary value and use that to reference your query

    <cfset realQuery = myQueries[queryName]>
    <cfoutput query="realQuery">
    ...
    </cfoutput>