我指的是以前的 SO Coldfusion Calculate Sum Total (Loop? ),它被标记为正确答案,但代码对我不起作用。
我正在尝试从多个领域创建一个总计。但我收到的错误是“未定义变量 xxx”。这是因为我试图从循环中为所有记录附加值
<cfoutput>
<cfquery name="ActivityReceipts" dbtype="query">
SELECT
activity,
qty_approved,
location,
payment_amount,
shipping_cost,
handling_cost
FROM rc.RC1
WHERE id_number = '#Receipts.id_number#'
</cfquery>
<cfloop query="ActivityReceipts">
<tr>
<td style="text-align:left;">#ActivityReceipts.activity#</td>
<td style="text-align:left;">#ActivityReceipts.qty_approved#</td>
<td style="text-align:left;">#ActivityReceipts.location#</td>
<td style="text-align: right; padding-right: 80px;">#ActivityReceipts.payment_amount#</td>
</tr>
<cfset grandTotal = grandTotal + ( #ActivityReceipts.payment_amount# + #ActivityReceipts.handling_cost# + #ActivityReceipts.Shipping_cost# ) />
</cfloop>
<td>#grandTotal#</td>
</cfoutput>
请注意,如果我将 grandTotal 变量设置行更改为
<cfset grandTotal = ( #ActivityReceipts.payment_amount# + #ActivityReceipts.handling_cost# + #ActivityReceipts.Shipping_cost# ) />
它不会导致错误,但它也只对最后一行求和,而不是对所有行求和。