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

发布于 2024-08-09 11:27:48 字数 313 浏览 5 评论 0原文

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

,例如:-

我有 ; 选择产品ID 来自产品

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

我如何在不使用的情况下打印它们 getProductId.产品名称 getProductId.Product_id

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就此别过 2024-08-16 11:27:48

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

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

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

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!

万劫不复 2024-08-16 11:27:48

您能澄清一下“不使用列名”是什么意思吗?

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

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

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.

云裳 2024-08-16 11:27:48

查询 HTML 表?有一个标签!

FTW!

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

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

<CFTable> FTW!

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

月隐月明月朦胧 2024-08-16 11:27:48

为了扩展我对 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文