Get sum of values in a loop?

Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Get sum of values in a loop?

 
0
  #1
Feb 3rd, 2008
This is partly PHP, partly javascript, so I wasn't sure where to stick it, but I'll stick it here. I'm sorry if this is a bit confusing, but I hope I can explain myself well.

The below code queries a table in my database, and prints out a table with those values. The embedded javascript enables you to change the values in some of the fields, and the other fields will automatically update using the "onchange" event handler. I'm a little experienced with php, and extremely new at javascript. Anyway, here's the code, and my question will follow:

  1. $query="SELECT * from categories";
  2. $result = mysql_query($query) or die(mysql_error());
  3.  
  4. while($row = mysql_fetch_array($result))
  5. {
  6. $balance=$row['balance']/100;
  7. $allotment=$row['allotment']/100;
  8. $total=$balance+$allotment;
  9. $name=$row['name'];
  10. $id=$row['id'];
  11. $left=$net-$sum;
  12.  
  13. echo "<tr>
  14. <td align='right'>$name</td>
  15. <td>$<input type='text' value='$balance' name='balance$id' readonly size=8 ></td>
  16. <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>
  17. <td>$<input type='text' value='$total' name='total$id' align='right' size=8 readonly></td>
  18. </tr>";
  19. }

So it basically prints out a table of values, but since they're in a loop, and not necessarily being pulled from my database, I don't know how to pull the totals. What I would like to do is pull the sum from the "allotment" field. Is this possible? Again, I'm sorry if I don't make any sense, but if anyone can help, I'd appreciate it.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Get sum of values in a loop?

 
0
  #2
Feb 3rd, 2008
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:

  1. SELECT sum( allotment ) FROM `categories`
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Get sum of values in a loop?

 
0
  #3
Feb 3rd, 2008
Yeah, sorry, I need to be more clear. I need to get the sum of the 'total$id' fields. I can get the sum of the allotment column from my database, but I need the sum of some of the totals that are calculated here in my table, that don't exist in my database. Thanks for the reply.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Get sum of values in a loop?

 
0
  #4
Feb 4th, 2008
Still not sure if I understand completely...

If you want to sum the totals you calculate in each loop with this line:
  1. $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...

  1. $query="SELECT * from categories";
  2. $result = mysql_query($query) or die(mysql_error());
  3. $calcTotal = array();
  4.  
  5. while($row = mysql_fetch_array($result))
  6. {
  7. $balance=$row['balance']/100;
  8. $allotment=$row['allotment']/100;
  9. $total = $balance+$allotment;
  10. $name=$row['name'];
  11. $id=$row['id'];
  12.  
  13. $calcTotal[$id] = $total; //stores each calculated total as array element with key of 'id'
  14.  
  15. $left=$net-$sum;
  16.  
  17. echo "<tr>
  18. <td align='right'>$name</td>
  19. <td>$<input type='text' value='$balance' name='balance$id' readonly size=8 ></td>
  20. <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>
  21. <td>$<input type='text' value='$ThisTotal' name='total$id' align='right' size=8 readonly></td>
  22. </tr>";
  23. }

you could then use
  1. 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
  1. $myTotal = $calcTotal[12] + $calcTotal[61]
...

sorry if I'm off track with what you're looking to accomplish!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: francine is an unknown quantity at this point 
Solved Threads: 0
francine francine is offline Offline
Newbie Poster

Re: Get sum of values in a loop?

 
0
  #5
Jun 29th, 2008
THANK YOU JONHSQUIBB! I was looking for that since a week!
:-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC