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

StackOverflow https://stackoverflow.com/questions/1642831

  •  10-07-2019
  •  | 
  •  

如何在不使用ColdFusion中的Results.columnname

的情况下打印所有结果

代表: -

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

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

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

谢谢,

有帮助吗?

解决方案

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

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

如果这不是你的意思,请道歉!

其他提示

如果不使用列名<!>“?

,请说明<!>”的含义

也许您想使用 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>

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

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

<CFTable> FTW!

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

为了扩展我对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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top