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