2016-12-07 75 views
1

我有一個簡單的「購物車」,由用戶更新詢價。他們陣列工作正常,附加效果很好。但由於某種原因,我似乎無法正確輸出數據到表格中。我的循環計數器正在工作,如果這是重要的:)請參閱下面的代碼,然後輸出我試圖去工作我知道這很簡單,我在想它。CF陣列的輸出

感謝

<cfif isDefined("url.Series")> 
    <cfset arrayAppend(session.cart, {Series = URL.Series , Style = URL.Style , Ohm = URL.Ohm , Notes = URL.Notes})> 
</cfif> 

<a href="cleararray.cfm">Clear Array</a><br /> 
<a href="Stylesearch.cfm">Style Search</a><br /><br /> 

<h1><b>DEBUG:</b></h1> 
<!--- Display current contents of cart ---> 
<cfdump var="#session.cart#" label="Cart Items"> 
<br /> 

<!--- Display items in cart in Table format ---> 
<table class="tftable" border="1"> 
    <tr> 
     <th>Series</th> 
     <th>Style ID</th> 
     <th>Exact &#8486;</th> 
     <th>Description</th> 
     <th>Notes</th> 
     <th>Quantity</th> 
     <th>Update</th> 
     <th>Delete</th> 
    </tr> 
    <cfloop index="Series" from="1" to="#arraylen(session.cart)#"> 
     <tr> 
     <td>#session.cart[Series]#</td> 
     <td>#Style#</td> 
     <td>#Ohm#</td> 
     <td>Test Description</td> 
     <td>#Notes#</td> 
     <td>Test Quantity</td> 
     <td>X</td> 
     <td>^</td> 
     </tr> 
    </cfloop> 
</table> 

CFDump

回答

1

你只需要一個CFOUTPUT來包裝你CFLOOP。

<cfoutput> 
<cfloop index="Series" from="1" to="#arraylen(session.cart)#"> 
    <tr> 
    <td>#session.cart[Series]#</td> 
    <td>#Style#</td> 
    <td>#Ohm#</td> 
    <td>Test Description</td> 
    <td>#Notes#</td> 
    <td>Test Quantity</td> 
    <td>X</td> 
    <td>^</td> 
    </tr> 
</cfloop> 
</cfoutput> 

個人而言,我也將改變循環索引比「系列」不同,因爲它可能會在你的車的結構的系列關鍵以後混亂。

輸出session.cart[Series]中的第一個單元格將成爲購物車中的第一個結構,而我認爲您想要的是: session.cart[Series].Series

這就是爲什麼我會循環索引更改爲s例如:

<cfoutput> 
<cfloop index="s" from="1" to="#arrayLen(session.cart)#"> 
<cfset thisRow = session.cart[s] /> 
<tr> 
<td>#thisRow.Series#</td> 
<td>#thisRow.Style#</td> 
<td>#thisRow.Ohm#</td> 
<td>Test Description</td> 
<td>#thisRow.Notes#</td> 
<td>Test Quantity</td> 
<td>X</td> 
<td>^</td> 
</tr> 
</cfloop> 
</cfoutput> 

希望有所幫助。

+0

Phipps,感謝這工作得很好,我有一點後續問題,一個例子可能會幫助我節省數小時。我將爲每一行編寫更新函數,我將如何去做這件事,因爲他們需要解決陣列的適當行。這就是表中的最後兩個字段(更新/刪除)。提前致謝。 –

+0

有很多方法可以做到這一點。您最好創建一個cart.cfc來處理項目的添加/更新/刪除。像這樣:[簡單購物車](https://www.bennadel.com/blog/637-ask-ben-creating-a-simple-wish-list-shopping-cart.htm)這有點舊,但是原則在那裏。然後,您需要決定是否要爲每次更新返回服務器(並重新加載整個頁面),或者使用異步JavaScript請求在後臺發佈數據並在沒有頁面重新加載的情況下更新購物車。 – Phipps73

+0

注意,如果你不需要任何索引號,你也可以使用'array'循環(而不是'from/to'),所以'index'變成當前數組元素,而不是位置。 – Leigh