• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Programming question

New Here ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

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!
TOPICS
Advanced techniques

Views

692

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

<cfoutput>#name.columnlist#</cfoutput>

See that doesn't give you any ideas.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

sorry, this line just gives me the column names (ie., red, blue, yellow). It does not display the data.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

Fantastic.

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

Regards!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

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#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 19, 2009 Feb 19, 2009

Copy link to clipboard

Copied

The second approach, <cfloop list="#cols#" index="col">, works really well. Thanks a bunch!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 20, 2009 Feb 20, 2009

Copy link to clipboard

Copied

>> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 20, 2009 Feb 20, 2009

Copy link to clipboard

Copied

LATEST
>> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation