how to loop through Query Columns in ColdFusion

霸气de小男生 提交于 2019-11-30 18:11:14

Just wanted to add Al Everett's solution returns the columns back in alphabetical order. If you would like to get the column names back in the same order as the query you can use:

ArrayToList( qrySE.getColumnNames() )

which I found here: http://www.richarddavies.us/archives/2009/07/cf_columnlist.php

you can use this to create a function to output queries to a table like this:

<cffunction name="displayQueryAsTable" output="true">   
    <cfargument name="rawQueryObject" type="query" required="true"> 
    <table >
    <tr>
        <cfloop list="#ArrayToList(rawQueryObject.getColumnNames())#" index="col" >
            <th>#col#</th>
        </cfloop>
    </tr>   
    <cfloop query="rawQueryObject">
        <tr>
            <cfloop list="#ArrayToList(rawQueryObject.getColumnNames())#" index="col">
                <td>#rawQueryObject[col][currentrow]#</td>
            </cfloop>
        </tr>   
    </cfloop>
    </table>        
</cffunction>

You could use the built-in query.columnList that is returned with each query. (It's metadata of the query, like recordCount.)

You could do something like this:

<table>
  <cfloop list="#qrySE.columnList#" index="col">
    <tr>
      <cfloop query="qrySE">
        <td>#qrySE[col][currentRow]#</td>
      </cfloop>
    </tr>
  </cfloop>
</table>

Not tested, but that should give you the idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!