2009-10-29 34 views
2

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

使用Results.columnname为前: -

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

在产品表我有2列与product_name和Product_id。

我怎样才能打印出来,而无需使用 getProductId.product_name getProductId.Product_id

感谢,

回答

5

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

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

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

+0

再次说明,不需要这个“行”变量!移动循环外的thead部分,然后执行查询cfloop! –

+0

@彼得 - 好点! –

+0

@Chris在这个如何才能提取出单列信息?我的意思是它是给我输出作为行明智,其中包含单行的每一列,我如何提取一行的所有列? –

2

能否请你澄清什么意思“而无需使用列名”?

也许你想使用getProductId.ColumnList属性?从我的旧代码转换查询到的阵列(有点剥离细节和改变变种名称,但给出了这个概念)

小例子:

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

编辑:增强的例子来摆脱连续可变的,正如注释中正确注意到的。

+0

不需要“行”变量。当做一个查询循环时,你已经有了'QueryName.CurrentRow'变量已经是 –

+0

@Peter,你是对的。这就是为什么我告诉它它是“旧代码”的一部分:) – Sergii

1

为了扩大我对克里斯的答复意见,这里的简单版本与缺失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>