ColdFusionでResults.columnnameを使用せずにすべての結果を印刷する方法

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

  •  10-07-2019
  •  | 
  •  

質問

ColdFusionでResults.columnnameを使用せずにすべての結果を印刷する方法

例:-

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

を持っています

Product Table iには、product_nameとProduct_idの2つの列があります。

使用せずに印刷するにはどうすればよいですか     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>

これが意図したものでない場合はお!びします!

他のヒント

列名を使用せずに<!> quot;の意味を明確にしてください<!> quot;?

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>

編集:コメントで正しく認識されているように、行変数を削除するための強化された例。

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