0

我想要做的是在最后一行得到总和。这是一个带有 2 组元素和总和的示例:

我有:

1000 (group number)

TitleUm   TitleTres   TitleQuatro
Apple     1           20
Pear      5           12

1001 (group number)

TV        1           20
Mobile    1           12
Car       1           15
Bicycle   1            5

 TOTAL   10           84

我在寻找:

1000 (group number)

TitleUm   TitleTres   TitleQuatro
Apple     1           20
Pear      5           12

 TOTAL    6           32

1001 (group number)

TV        1           20
Mobile    1           12
Car       1           15
Bicycle   1            5

 TOTAL    4           52

下面的代码几乎做了所有事情,列出了每行的项目和每个值,但它现在确实显示了总和:

<table class="Itens">
<%
currentGroupName = ""
previousGroupName = ""
Do Until ItensList.EOF
currentGroupName = ItensList("oito")
Um= ItensList("um")
Tres= ItensList("tres")
Quatro= CCur(ItensList("quatro"))

If currentGroupName <> previousGroupName Then
%>

<tr>
<td><% Response.Write currentGroupName %></td>
</tr>
<tr>            
<th><% Response.Write TituloUm %></th>
<th><% Response.Write TituloTres %></th>
<th><% Response.Write TituloQuatro %></th>
</tr>

<%
End If
%>

<%
Um= CCur(ItensList("um"))
Tres= CCur(ItensList("tres"))
Quatro= CCur(ItensList("quatro"))
%>

<tr>
<td><% Response.Write Um %></td>
<td><% Response.Write Tres %></td>
<td><% Response.Write Quatro %></td>
</tr>

<%
previousGroupName = currentGroupName
ItensList.MoveNext
Loop
ItensList.Close
%>

<tr class="Total">
<td>TOTAL</td>
<td><% Response.Write ShowSumTotalTresHere %></td>
<td><% Response.Write ShowSumTotalQuatroHere %></td>
</tr>
</table>

谢谢您的帮助

4

2 回答 2

0

将每个添加到循环内的变量中,如下所示:

<%
SumSomething = 0

Do Until objRs.EoF
     SumSomething = SumSomething + objRs("SomeValue")
objRs.MoveNext
Loop
%>
...
<tr>
  <td><%= SumSomething%></td>
...
于 2013-01-14T13:23:10.297 回答
0

您需要在循环时保持一个运行总数,以下面的示例代码为例,我在循环之前声明了两个新变量:

Total_Tres          = 0
Total_Quatro        = 0

然后我在你的循环中增加这些:

'Maintain Running Total
Total_Tres          = Total_Tres + Tres
Total_Quatro        = Total_Quatro + Quatro

最后,输出总变量:

<td>TOTAL</td>
<td><% = Total_Tres %></td>
<td><% = Total_Quatro %></td>

完整示例(未经测试)

<table class="Itens">
<%
currentGroupName    = ""
previousGroupName   = ""
Total_Tres          = 0
Total_Quatro        = 0
Do Until ItensList.EOF
    currentGroupName    = ItensList("oito")
    Um                  = ItensList("um")
    Tres                = ItensList("tres")
    Quatro              = CCur(ItensList("quatro"))

    If currentGroupName <> previousGroupName Then
        %>
        <tr>
            <td><% = currentGroupName %></td>
        </tr>
        <tr>            
            <th><% = TituloUm %></th>
            <th><% = TituloTres %></th>
            <th><% = TituloQuatro %></th>
        </tr>
        <%
    End If

    Um      = CCur(ItensList("um"))
    Tres    = CCur(ItensList("tres"))
    Quatro  = CCur(ItensList("quatro"))
    %>
    <tr>
        <td><% = Um %></td>
        <td><% = Tres %></td>
        <td><% = Quatro %></td>
    </tr>
    <%
    '#### Maintain Running Total
    Total_Tres          = Total_Tres + Tres
    Total_Quatro        = Total_Quatro + Quatro

    '### Flag current group
    previousGroupName   = currentGroupName
    ItensList.MoveNext
Loop
ItensList.Close
%>
<tr class="Total">
<td>TOTAL</td>
<td><% = Total_Tres %></td>
<td><% = Total_Quatro %></td>
</tr>
</table>
于 2013-01-14T13:23:40.923 回答