views:

351

answers:

5

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,

+1  A: 

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.

Sergii
The `row` variable is not needed. When doing a query loop, you've got the `QueryName.CurrentRow` variable already
Peter Boughton
@Peter, you're right. That's why I told that its a piece of the "old code" :)
Sergii
+4  A: 

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!

Chris R
Again, there is no need for this `row` variable! Move the thead section outside the loop and do a query cfloop instead!
Peter Boughton
@Peter - good point!
Chris R
A: 

Thats a Greate Responce :)

That i sthe same I am looking for , I dont know column names And i want tp ptint what i have in resultquery.

Both solutions are fine for me..

Thanks for the greate Help Sergii and Chris

CFUser
+1  A: 

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>
Peter Boughton
+1  A: 

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

<CFTable> FTW!

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

Henry