2012-11-02 3 views
2

ColdFusion에서 데이터 업데이트를 허용하는 스코어 카드를 작성하고 있습니다. 행과 열로 데이터를 표시해야합니다. 다음은 테이블의 예는 다음과 같습니다 어떤 도움을 주시면 감사하겠습니다ColdFusion - 열을 행으로 표시

Metric Jan Feb Mar Apr 

Met1 15 30 15 16 
Met2 24 21 19 31 
Met3 21 14 20 33 
Met4 40 39 28 21 
Met5 50 44 19 43 

:

Mon Met1 Met2 Met3 Met2 Met5 
Jan 15 24 21 40 50 
Feb 30 21 14 39 44 
Mar 15 19 20 28 19 
Apr 16 31 33 21 43 

내가 데이터를 다음과 같이 (행과 열을 틀지)을 표시합니다. 감사합니다.

+0

최대 열 몇 개입니까? 테이블 구조를 바꿀 수 있습니까 (예 : 정규화)? 또한 사용중인 데이터베이스를 나타내는 질문 태그를 업데이트하십시오. – Leigh

+0

테이블 열에 추가 또는 삭제가있을 수 있지만 행은 14 : 1 - 12 월과 GOAL 및 YTD로 유지됩니다. 그 다음에는 정상화되지 않습니다. 테이블 databse MS SQL 및 MySQL 것입니다. 고맙습니다 – dnut

+0

당신은 정말로 SQL 서버와 MySQL을 모두 사용하고 있습니까? 복잡한 기능/운영자는 특정 공급 업체의 경향이 있습니다. 예를 들어 MS SQL에서는 '피벗 (pivot)'을 사용할 수 있습니다. 그러나 afaik MySQL은 아직 지원하지 않습니다. – Leigh

답변

1

'세로 정렬'과 같은 소리 나는 몇 년 전에 썼습니다. 아마도 도움이 될 것입니다 :

<html> 
<head> 
    <title>Vertical Sorting</title> 
</head> 
<cfquery name = "qMyQuery" datasource = "dsn"> 
    SELECT fields 
    FROM table 
    ORDER BY myField 
</cfquery> 
<body> 
<!--- set the number of colums you wish to have ---> 
<cfset cols = 5> 
<!--- get the number of rows so you know what record to display at the top of the next row. for example if our query contains "a,b,c,d,e,f,g,h,i,j,k,l,m" (13 elements) it will produce 3 totalrows---> 
<cfset totalRows = ceiling(qMyQuery.RecordCount/cols)> 
<!--- set inital record to 1 "output" is the actual cell of the query ---> 
<cfset output = 1> 
<!--- Create table ---> 
<table width = "100%" border="0" align="center" cellpadding="2" cellspacing = "2">    
    <!--- loop through the rows. This loop will run 3 times in this example ---> 
    <cfloop from = "1" to = "#totalRows#" index = "thisRow"> 
    <tr> 
     <!--- this loop will run 5 times in times in this example ---> 
     <cfloop from = "1" to = "#cols#" index = "thisCol"> 
     <!--- the width in the table cell will dynamicaly calculated to evenly distribute the cells. in this example if cols = 5 100/5 will make the cells 20% of the table ---> 
     <td width = "<cfoutput>#numberformat((100/cols), 99)#</cfoutput>%" align="center" nowrap style = "border: 1px solid #ccc;"> 
      <!--- Check current record with the record count, this will be used to display data or an empty cell ---> 
      <cfif output lte qMyQuery.recordCount> 
       <cfoutput>#qMyQuery.Mon[output]#</cfoutput> 
      <cfelse> 
      <!--- use <br> to display an empty cell ---> 
       <br> 
      </cfif> 
      <!--- increment counter to the next record in this example if we started on the first cell of the first row it would be 1(a), then 4(d), then 7(g) and so on if this was the firs cell on the second row it would be 2(b), 5(e), 8(h), continue... ---> 
      <cfset output = output + totalRows> 
     </td> 
     </cfloop> 
     <!--- this little bit tells where to start the next row. if we just finished the first row output would be 2(b) ---> 
     <cfset output = thisRow + 1> 
    </tr> 
    </cfloop> 
</table> 
</body> 
</html> 
+0

트래비스가 도와 줘서 고마워. 이 데이터를 샘플 데이터에 적용하면 "undefined" "Mon"이라는 줄에 오류가 발생합니다. # qMyQuery.Mon [출력] #. 아마도 Mod라고 생각 했는데도 작동하지 않았습니다. 다시 한번 감사드립니다. – dnut

+0

은 열 이름입니까? – Travis

+0

의미 ... # qMyQuery.Mon [출력] #은 필자가 작성한 샘플 코드이므로 사용자 고유의 쿼리 및 열 이름을 입력해야합니다. – Travis

관련 문제