Jump to content

Get the sum of a column in a repeated recordset that holds a variable and echos a value


bna747

Recommended Posts

<table border="1">

  <tr>

    <td>test_id</td>

    <td>fruits</td>

    <td>vegetables</td>

    <td>meats</td>

    <td>poultry</td>

    <td>Totals</td>

  </tr>

  <?php do { ?>

    <tr>

      <td><?php echo $row_Recordset1['test_id']; ?></td>

      <td><?php echo $row_Recordset1['fruits']; ?></td>

      <td><?php echo $row_Recordset1['vegetables']; ?></td>

      <td><?php echo $row_Recordset1['meats']; ?></td>

      <td><?php echo $row_Recordset1['poultry']; ?></td>

      <td><?php 

 $test1=$row_Recordset1['fruits'];;

 $test2=$row_Recordset1['vegetables'];

 $test3=$row_Recordset1['meats'];

 $test4=$row_Recordset1['poultry'];

 

 $testtotal=$test1+$test2+$test3+$test4;

   if ($testtotal == '0') {

echo '0.00';

}else {

 

 

echo "$" . number_format($testtotal, 2, '.', ',');

}

 

?></td>

    </tr>

    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

</table>

 

<div> Need the sum of all in the "Totals" column to echo here (value should be $84.00) </div>

 

I have a table that has values in each column and php code to add up all values in each row to output the total dollar amount on each row. I having a very difficult time trying to echo out the sum of all rows only in the "TOTALS" column. The totals column of the table is not a field in the database table that stores the totaltest-table.tiff. Can anyone help with a solution to this problem? 

So want the add up the subtotals of $testtotal.

 

You'll wan't to have another variable, call this variable $total_sum.

 

So after

 $testtotal=$test1+$test2+$test3+$test4;

add

$total_sum += $testtotal;

Then before the </table> add 

<tr><td colspan="5"></td><td><?php echo $total_sum; ?></td></tr> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.