2015-10-05 58 views
0

我想遍歷查詢並對每一行迭代顯示每行兩個id。我在一個循環內嘗試過一個循環,但它只輸出所有行兩次。循環內的CFloop

<!--- sample query ---> 
<cfset sheets = queryNew("")> 
<cfset queryAddColumn(sheets, "id", [1,2,3,4])> 

<table> 
<cfoutput> 
<cfloop query="sheets"> 
<tr> 
    <cfloop query="sheets"> 
     <td align="center">#sheets.id#</td> 
    </cfloop> 
</tr> 
</cfloop> 
</cfoutput> 
</table> 

回答

1

嵌套循環不是這樣做的方式。使用mod函數的條件邏輯更好。這是一個簡單的例子。

<cfloop query = "sheets"> 
<tr> 
td cells and data go here: 
<cfif currentrow mod 2 is 0> 
</tr> 
<tr> 
</cfif> 

請注意,此答案有意不完整。我要離開的部分是如何處理查詢有奇數行的情況。

+0

非常好,非常感謝 – user2620804