funbinod Posted July 31, 2015 Share Posted July 31, 2015 I've a query that gets some rows with some results. and I want to sum all the results like I get $balance from a query while ($r = $q->fetch_array) { echo $balance.'<br />';} and when I get the rows with $balance, I want to sum all of them and display at the bottom I tried, $total = 0;while ($r = $q->fetch_array) { $total = $balance++; echo $balance.'<br /';}echo $total; but its not giving the required result. please guide. thanks in advance.... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 31, 2015 Share Posted July 31, 2015 Doing $total = $balance++; is going to increment $balance by 1, and then set $total to that value. You want to add $balance to total instead. You can also do the sum in SQL. Link to comment Share on other sites More sharing options...
funbinod Posted July 31, 2015 Author Share Posted July 31, 2015 I also tried $total += $balance; and it also doesn't work. actually the $balance is derived from lots of calculations after the query so I don't know how can I make the sum of $balance in sql. that's why I wish the $total calculated with php... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 31, 2015 Share Posted July 31, 2015 I also tried$total += $balance;and it also doesn't work.That's going to add $balance to $total. Sounds like that's what you want. I'm not sure what you're doing where that wouldn't work.actually the $balance is derived from lots of calculations after the query so I don't know how can I make the sum of $balance in sql.I figured, because your first code block doesn't show where you're getting $balance from, but you can do a lot of calculations with SQL. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now