2016-12-07 45 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