Jump to content

Sum arrays


tjverge

Recommended Posts

I'm not sure if this can be done, but I would like to get the sum for each $.row[''] result ex: if $row['robotics'] had values of 100,150,300 I would like to be able to show a total of 550

 

<?php
echo "<Table border=1>";
echo "<tr><td></td><td>Robotics</td><td>Chiral Stuctors</td><td>Enriched Uranium</td><td>Mechanical Parts</td><td>Coolant</td><td>Consumer Electronics</td><td>Precious Metals</td><td>Reactive Metals</td><td>Oxygen</td><td>Toxic Metals</td></tr>";
$result = mysql_query("select * from `pi`");
while ($row = mysql_fetch_array($result)) {

echo "<tr><td>".$row['pilot']."</td>";
echo "<td>".$row['robotics']."</td>";
echo "<td>".$row['chiralstuctors']."</td>";
echo "<td>".$row['enricheduranium']."</td>";
echo "<td>".$row['mechanicalparts']."</td>";
echo "<td>".$row['collant']."</td>";
echo "<td>".$row['consumerelectronics']."</td>";
echo "<td>".$row['preciousmetals']."</td>";
echo "<td>".$row['reactivemetals']."</td>";
echo "<td>".$row['oxygen']."</td>";
echo "<td>".$row['toxicmetals']."</td></tr>";

}
echo "</table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/227020-sum-arrays/
Share on other sites

You should be able to just keep a running total in the while loop, if I understand you correctly.

 

$result = mysql_query("select * from `pi`");

$total = 0;

while ($row = mysql_fetch_array($result)) {

echo "<tr><td>".$row['pilot']."</td>";
echo "<td>".$row['robotics']."</td>";
echo "<td>".$row['chiralstuctors']."</td>";
echo "<td>".$row['enricheduranium']."</td>";
echo "<td>".$row['mechanicalparts']."</td>";
echo "<td>".$row['collant']."</td>";
echo "<td>".$row['consumerelectronics']."</td>";
echo "<td>".$row['preciousmetals']."</td>";
echo "<td>".$row['reactivemetals']."</td>";
echo "<td>".$row['oxygen']."</td>";
echo "<td>".$row['toxicmetals']."</td></tr>";

$total += $row['robotics'];

}
echo "</table>";

Link to comment
https://forums.phpfreaks.com/topic/227020-sum-arrays/#findComment-1171284
Share on other sites

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.