I'm not sure I understand everything you're trying to do, but if you want the sum of all the rows' allotment columns, you can use the sum() mysql function.
for example:
SELECT sum( allotment ) FROM `categories`
johnsquibb
Junior Poster in Training
84 posts since Nov 2007
Reputation Points: 14
Solved Threads: 14
Still not sure if I understand completely...
If you want to sum the totals you calculate in each loop with this line:
$total=$balance+$allotment;
you could instead use an array to hold each of the totals, and then add all/some of the elements together later...for example, you could modify your code to something like this...
$query="SELECT * from categories";
$result = mysql_query($query) or die(mysql_error());
$calcTotal = array();
while($row = mysql_fetch_array($result))
{
$balance=$row['balance']/100;
$allotment=$row['allotment']/100;
$total = $balance+$allotment;
$name=$row['name'];
$id=$row['id'];
$calcTotal[$id] = $total; //stores each calculated total as array element with key of 'id'
$left=$net-$sum;
echo "<tr>
<td align='right'>$name</td>
<td>$<input type='text' value='$balance' name='balance$id' readonly size=8 ></td>
<td>$<input type='text' value='$allotment' name='allotment$id' size=8 onchange='total$id.value = ((allotment$id.value*100) + (balance$id.value*100))/100;'></td>
<td>$<input type='text' value='$ThisTotal' name='total$id' align='right' size=8 readonly></td>
</tr>";
}
you could then use
array_sum($calcTotal);
to add all the values that you calculated, or you could add only the ones you want by calling the individual keys, such as
$myTotal = $calcTotal[12] + $calcTotal[61]
...
sorry if I'm off track with what you're looking to accomplish!
johnsquibb
Junior Poster in Training
84 posts since Nov 2007
Reputation Points: 14
Solved Threads: 14