x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 171

I have a small sum that loops for each individual item. Is there a way to total the subtotals though?

``````<?php
\$theqty = \$row_Recordset1['qtyy'];
\$theprice = \$row_Recordset1['qprice'];
\$thetotal = \$theqty * \$theprice;
echo \$thetotal;
?>
``````
0
jonofat
• 4
• 3
1 Solution

Commented:
There is no loop shown in the code sample, so there would not be any subtotals.
0

Author Commented:
Sorry..

`````` <?php \$i=0; do { ?>
<tr>
<td class="bottomborder"> <input name="record[<?php echo \$i ?>][qtyy]" value="<?php echo \$row_Recordset1['qtyy']; ?>" size="1" />
x <?php echo \$row_Recordset1['pdescription']; ?>
<input name="record[<?php echo \$i ?>][productidd]" type="hidden" id="record[<?php echo \$i ?>][productidd]" value="<?php echo \$row_Recordset1['productidd']; ?>" /></td>
<td class="bottomleft">R <?php echo \$row_Recordset1['qprice']; ?> x <?php echo \$row_Recordset1['qtyy']; ?>= <span class="bottomborder">
R<?php \$theqty = \$row_Recordset1['qtyy'];
\$theprice = \$row_Recordset1['qprice'];
\$thetotal = \$theqty * \$theprice;
echo \$thetotal; ?>
ex. vat
<input name="totalprice" type="hidden" id="totalprice" value="<?php echo \$thetotal; ?>" />
</span></td>
</tr>
<?php \$i++; } while (\$row_Recordset1 = mysql_fetch_assoc(\$Recordset1)); ?>
``````
0

Commented:
How would the subtotals be grouped (what column name)?
0

Author Commented:
Well, it wouldn't be a column name I would use, wouldn't it be \$thetotal ?

\$thetotal = \$theqty * \$theprice;

which gives a subtotal for each line. But I need to add the subtotal of each line together. So if there are 3 individual products that look like this:

2 x \$4 = \$8
1 x \$3 = \$3
3 x \$4 = \$12

I somehow need it to add those up to give me \$23.

I don't know if it would be some kind of foreach loop with \$thetotal or what?
0

Author Commented:
I took a wild guess with this but it didn't get me anywhere.

\$sum = 0;
foreach(\$thetotal as \$value) \$sum = \$sum + \$value;
echo \$sum;
0

Commented:
OK, I think I get it better now.  This may not be exact (I can't test it), but it should be pretty close.
``````<?php

// INITIALIZE A VARIABLE
\$sum_total = 0;

\$i=0;

do { ?>
<tr>
<td class="bottomborder"> <input name="record[<?php echo \$i ?>][qtyy]" value="<?php echo \$row_Recordset1['qtyy']; ?>" size="1" />
x <?php echo \$row_Recordset1['pdescription']; ?>
<input name="record[<?php echo \$i ?>][productidd]" type="hidden" id="record[<?php echo \$i ?>][productidd]" value="<?php echo \$row_Recordset1['productidd']; ?>" /></td>
<td class="bottomleft">R <?php echo \$row_Recordset1['qprice']; ?> x <?php echo \$row_Recordset1['qtyy']; ?>= <span class="bottomborder">
R<?php \$theqty = \$row_Recordset1['qtyy'];
\$theprice = \$row_Recordset1['qprice'];
\$thetotal = \$theqty * \$theprice;
echo \$thetotal;

\$sum_total = \$sum_total + \$thetotal;

?>
ex. vat
<input name="totalprice" type="hidden" id="totalprice" value="<?php echo \$thetotal; ?>" />
</span></td>
</tr>
<?php

\$i++;
} while (\$row_Recordset1 = mysql_fetch_assoc(\$Recordset1));

// AT END OF LOOP, SHOW THE VARIABLE
var_dump(\$sum_total);

?>
``````
0

Author Commented:
Tested it. Works perfectly. Genius! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.