如何在ColdFusion中循环查询列 [英] how to loop through Query Columns in ColdFusion
问题描述
我在我的CFC中有一个查询。该函数包含一个简单的查询。
I have a query in a my CFC. The function contains a simple query as such.
<cfquery name="qrySE" datasource=#mydatasource#>
SELECT
NAMES,SALARY
FROM tblTest
</cfquery>
我想显示我的结果集(水平):
I want to display my resultset as such (horizontally):
NAME1 NAME2 NAME3 NAME4
10 20 45 62
方法循环查询的列,并为此目的创建一个虚拟查询?
Is there a way to loop through the columns of my query and create a virtual query for this purpose?
如果有人这样做,请让我知道。
If anyone has done this, please let me know.
推荐答案
只是想添加Al Everett的解决方案按字母顺序返回列。如果您希望以与查询相同的顺序返回列名称:
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() )
我在这里找到: 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>
这篇关于如何在ColdFusion中循环查询列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!