Skip to main content
Participating Frequently
February 19, 2009
Question

Programming question

  • February 19, 2009
  • 10 replies
  • 791 views
I have a program starting with the following codes:

<cfset cols='red,blue,yellow'>
<cfquery datasource="datasource" name="name">
select #cols# from a_table
</query>

My question is, how can I display the queried dataset without referencing the elements in cols?

Regards!
This topic has been closed for replies.

10 replies

Inspiring
February 20, 2009
>> There is a way to preserve the original order?
> Yes, there is, but I do not know it.

getMetadata()
http://livedocs.adobe.com/coldfusion/8/functions_e-g_50.html

--
Adam
Inspiring
February 20, 2009
>> sorry, this line just gives me the column names (ie., red, blue, yellow). It does not display the data.
>
> No it doesn't, but didn't it give you some ideas on how it might.

There's some docs here:
http://livedocs.adobe.com/coldfusion/8/Variables_18.html

--
Adam
NatomasAuthor
Participating Frequently
February 20, 2009
The second approach, <cfloop list="#cols#" index="col">, works really well. Thanks a bunch!!!
Inspiring
February 19, 2009
There is probably a better way, but the only way I've found to preserve the order is to set the order like you did in the cols variable, then put the query into an array with the col name as one of the fields, and then sort them out based on the cols variable. Bit clunky but it works.
Inspiring
February 19, 2009
Or this:

<cfset cols='red,blue,yellow'>
<cfquery datasource="datasource" name="name">
select #cols# from a_table
</query>

<cfloop query="name">
<cfloop list="#cols#" index="col">
<cfoutput>#name[col][name.currentrow]#</cfoutput>
</cfloop>
</cfloop>
Inspiring
February 19, 2009
Natomas wrote:
> There is a way to preserve the original order?
>


Yes, there is, but I do not know it.

If you don't have the original variable that formed the SQL statement,
you will need to do some searching. There is someway to do this.

You might explore this output a bit.

<cfset cols='red,blue,yellow'>
<cfquery datasource="datasource" name="name" result="george">
select #cols# from a_table
</query>

<cfdump var="#george#">
NatomasAuthor
Participating Frequently
February 19, 2009
Fantastic.

One more question, the columnlist is alphabetically ordered. There is a way to preserve the original order?

Regards!
Inspiring
February 19, 2009
Natomas wrote:
> sorry, this line just gives me the column names (ie., red, blue, yellow). It does not display the data.

No it doesn't, but didn't it give you some ideas on how it might.

Of course unless you just want to dump the data. For that there is the
<cfdump var="#name#"> tag.

I presumed you would want some formatting of you your data. Here is a
larger hint on how that might be done.

<cfloop query="name">
<cfloop list="#name.columnlist#" index="col">
<cfoutput>#name[col][name.currentrow]#</cfoutput>
</cfloop>
</cfloop>

NatomasAuthor
Participating Frequently
February 19, 2009
sorry, this line just gives me the column names (ie., red, blue, yellow). It does not display the data.
Inspiring
February 19, 2009
<cfoutput>#name.columnlist#</cfoutput>

See that doesn't give you any ideas.