Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Column Names

Status
Not open for further replies.

riluve

Technical User
Mar 11, 2005
78
US
I'm sifting through data from a query and I want to be able to address the column name dynamically, but I can't seem to figure out the syntax.

e.g. if i have 10 'columns' returned in a query I can refer to each by name:

<cfset x[1] = query.column1>
<cfset x[2] = query.column2>
...
<cfset x[3] = query.column10>

But I would rather put them in a proper loop:

<cfloop index="i" from="1" to= "10">
<cfset x = query.column#i#>
</cfloop>

Shouldn't there be am esy way to do this? I am sure I am just missing something because it doesn't like the embedded #'s in the cfset.



 
try this:

<cfset qryData = QueryNew("a,b,c,d,e,f,g")>

<cfset temp = QueryAddRow(qryData, 2)>

<cfset temp = QuerySetCell(qryData, "a", "1", 1)>
<cfset temp = QuerySetCell(qryData, "b", "2", 1)>
<cfset temp = QuerySetCell(qryData, "c", "3", 1)>
<cfset temp = QuerySetCell(qryData, "d", "4", 1)>
<cfset temp = QuerySetCell(qryData, "e", "5", 1)>
<cfset temp = QuerySetCell(qryData, "f", "6", 1)>
<cfset temp = QuerySetCell(qryData, "g", "7", 1)>
<cfset temp = QuerySetCell(qryData, "a", "7", 2)>
<cfset temp = QuerySetCell(qryData, "b", "6", 2)>
<cfset temp = QuerySetCell(qryData, "c", "5", 2)>
<cfset temp = QuerySetCell(qryData, "d", "4", 2)>
<cfset temp = QuerySetCell(qryData, "e", "3", 2)>
<cfset temp = QuerySetCell(qryData, "f", "2", 2)>
<cfset temp = QuerySetCell(qryData, "g", "1", 2)>

<cfoutput>
<cfloop from="1" to="#qryData.RecordCount#" index="i">
<cfloop list="#qryData.ColumnList#" index="j">
#j#&nbsp;&nbsp;#qryData[j]#<br />
</cfloop>
</cfloop>
</cfoutput>

Hope this helps!

Tony
 
If you're wanting to name it the same thing as the column name, try something like this:
Code:
<cfset N = 0>
<cfloop index="i" list="#query_name.columnList#">
  <cfset N = N+1>
  <cfset x[N] = i>
</cfloop>


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top