2

如何在 ColdFusion 中不使用 Results.columnname 打印所有结果

例如:-

我有<cfquery name="getProductId"> select productId from product </cfquery>

在产品表中,我有 2 列带有 product_name 和 Product_id。

如何在不使用 getProductId.product_name getProductId.Product_id 的情况下打印它们

谢谢,

4

4 回答 4

6

你想达到什么目的?如果您正在寻找一种基于您不知道列名的查询来计算输出查询结果的方法,例如...

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

...然后您可以使用该queryName.ColumnList变量,该变量返回所有列名的逗号分隔列表。您随后可以遍历此列表,并根据需要进行输出。

例如,要获得一个简单的 HTML 表格输出:

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

抱歉,如果这不是您的意思!

于 2009-10-29T11:21:29.507 回答
2

您能否澄清“不使用列名”的含义?

也许您想使用getProductId.ColumnList属性?

我的旧代码中将查询转换为数组的小示例(有点剥离细节并更改了 var 名称,但显示了这个想法):

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

编辑:删除行变量的增强示例,正如评论中正确注意到的那样。

于 2009-10-29T11:16:48.093 回答
2

查询到 HTML 表?有一个标签!

<CFTable>FTW!

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

于 2009-10-29T21:36:41.907 回答
1

为了扩展我对 Chris 的回复的评论,这里是添加了缺少的 thead/tbody 标签的更简单的版本:

<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>
于 2009-10-29T13:24:22.827 回答