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? 
Link to comment
Share on other sites

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> 
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.