CFUser
CFUser

Reputation: 2346

How to print all the result without using Results.columnname in ColdFusion

How to print all the result without using Results.columnname in ColdFusion

for ex:-

I have <cfquery name="getProductId"> select productId from product </cfquery>

In Product Table i have 2 columns with product_name and Product_id.

How can I print them without using getProductId.product_name getProductId.Product_id

Thanks,

Upvotes: 2

Views: 5722

Answers (4)

Henry
Henry

Reputation: 32885

Query to HTML table? there's a tag for that!

<CFTable> FTW!

http://www.cfquickdocs.com/cf8/#cftable :)

Upvotes: 2

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

Can you please clarify what means "without using column name"?

Maybe you want to use the getProductId.ColumnList attribute?

Small example from my old code that converts query to the array (a bit stripped details and changed var names, but shows the idea):

    <cfset arrRecordSet = ArrayNew(1)>

    <cfloop query="qGetSomething">
        <cfset record = StructNew()>
        <cfloop list="#qGetSomething.ColumnList#" index="field">
            <cfset record[field] = qGetSomething[field][qGetSomething.CurrentRow]>
        </cfloop>
        <cfset ArrayAppend(arrRecordSet,record)>
    </cfloop>

EDIT: enhanced example to get rid of row variable, as correctly noticed in comments.

Upvotes: 2

Peter Boughton
Peter Boughton

Reputation: 112150

To expand on my comment to Chris's response, here's the simpler version with the missing thead/tbody tags added:

<cfoutput>
    <table>
        <thead>
            <tr>
                <cfloop index="ColName" list="#MyQuery.ColumnList#">
                    <th>#ColName#</th>
                </cfloop>
            </tr>
        </thead>
        <tbody>
            <cfloop query="MyQuery">
                <tr>
                    <cfloop index="ColName" list="#MyQuery.ColumnList#">
                        <td>#MyQuery[ColName][MyQuery.CurrentRow]#</td>
                    </cfloop>
                </tr>
            </floop>
        </tbody>
    </table>
</cfoutput>

Upvotes: 1

Chris R
Chris R

Reputation: 833

What are you trying to achieve? If you are looking for a way to computationally output query results based on a query whose column names you do not know, such as...

<cfquery name="queryName" ...>
    select * from product
</cfquery>

...then you can use the queryName.ColumnList variable, which returns a comma separated list of all column names. You could subsequently iterate over this list, and output as required.

For example, to get a simple HTML table output:

<table border=1>
    <cfloop from="0" to="#queryName.RecordCount#" index="row">
        <cfif row eq 0>
            <tr>
                <cfloop list="#queryName.ColumnList#" index="column" delimiters=",">
                    <th><cfoutput>#column#</cfoutput></th>  
                </cfloop>
            </tr>
        <cfelse>
            <tr>
                <cfloop list="#queryName.ColumnList#" index="column" delimiters=",">
                    <td><cfoutput>#queryName[column][row]#</cfoutput></td>
                </cfloop>
            </tr>
    </cfif>
    </cfloop>
</table>

Apologies if this isn't what you meant!

Upvotes: 6

Related Questions