| | |
Get sum of values in a loop?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
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.
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:
php Syntax (Toggle Plain Text)
$query="SELECT * from categories"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $balance=$row['balance']/100; $allotment=$row['allotment']/100; $total=$balance+$allotment; $name=$row['name']; $id=$row['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='$total' name='total$id' align='right' size=8 readonly></td> </tr>"; }
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.
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:
for example:
PHP Syntax (Toggle Plain Text)
SELECT sum( allotment ) FROM `categories`
Still not sure if I understand completely...
If you want to sum the totals you calculate in each loop with this line:
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...
you could then use to add all the values that you calculated, or you could add only the ones you want by calling the individual keys, such as ...
sorry if I'm off track with what you're looking to accomplish!
If you want to sum the totals you calculate in each loop with this line:
PHP Syntax (Toggle Plain Text)
$total=$balance+$allotment;
php Syntax (Toggle Plain Text)
$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
PHP Syntax (Toggle Plain Text)
array_sum($calcTotal);
PHP Syntax (Toggle Plain Text)
$myTotal = $calcTotal[12] + $calcTotal[61]
sorry if I'm off track with what you're looking to accomplish!
![]() |
Similar Threads
- C++ Calculating even Values Problem (C++)
- how to sum the cells in excel using asp (ASP)
- CSV file (C)
- How to assign my own numeric values to a char array? (C++)
- PseudoCode Help For Newbie (Computer Science)
- Any Boolean Expression to Sum of Minterms (C++)
- while loop in c++ help needed (C++)
Other Threads in the PHP Forum
- Previous Thread: php text file read
- Next Thread: delete multiple record via echo checkbox
Views: 3771 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class clean cms code countingeverycharactersfromastring cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail match menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions simple sms soap source space spam speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





