는 방법을 인쇄하는 모든 결과를 사용하지 않고 결과입니다.열 이름 ColdFusion
-
10-07-2019 - |
문제
는 방법을 인쇄하는 모든 결과를 사용하지 않고 결과입니다.열 이름 ColdFusion
ex:-
나 <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>
편집:향상된 예제를 제거하의 행 변수,으로 올바르게 나타났다.
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>