stormx Posted March 14, 2009 Share Posted March 14, 2009 Hello, I need a little assistance with PHP and Maths, I want to be able to total up digits with two decimal places, I tried writing my own script, and here it is below: <?php $result = mysql_query("SELECT * FROM invoices WHERE userid='4'" ); $num_rows = mysql_num_rows($result); echo "$num_rows Invoices\n"; ?> $<?php while($history = mysql_fetch_array($result)) { $total += $history['total']; echo $total; } ?> I get the following output: 3 Invoices $6.9513.940.85 The total of invoices is correct, but the amount of them isn't. This was what is under userid 4 in the database for total: 6.95 6.95 26.95 I just want to plus them all together, but my code is seeming to give more than it should, please help me Thanks. Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/ Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 what column type is "total" set to? and do you initialize $total =0 anywhere ? Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784277 Share on other sites More sharing options...
stormx Posted March 14, 2009 Author Share Posted March 14, 2009 Well I added $total =0 But now I'm just getting the amounts 3 Invoices $6.956.9526.95 (break it down: 3 Invoices $6.95 6.95 26.95) So it's getting the amounts correctly, just not combining them together. Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784280 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 actually, that is outputting correctly. you just need to either put a newline after your echo, or move the echo outside the loop Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784286 Share on other sites More sharing options...
stormx Posted March 14, 2009 Author Share Posted March 14, 2009 Well I put the echo outside the loop, but now I'm just getting $26.95, to make it clear I just want the script to do this: 6.95 + 6.95 + 26.95 = $40.85 Thanks Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784289 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 that is what you were getting, you were just outputting too much, see from your original post 3 Invoices $6.9513.940.85 Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784292 Share on other sites More sharing options...
kittrellbj Posted March 14, 2009 Share Posted March 14, 2009 I would do something like this: <?php $result = mysql_query("SELECT * FROM invoices WHERE userid='4'" ); $num_rows = mysql_num_rows($result); echo "$num_rows Invoices\n"; ?> $<?php for($i = 0; $i <= $num_rows; $i++) { $total += $history['total']; return $total; } echo $total; ?> That should do it, but not tested. Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784294 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 you dont return from a for loop, its not a function ? Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784295 Share on other sites More sharing options...
stormx Posted March 14, 2009 Author Share Posted March 14, 2009 Thanks for your help Next time I'll remember to place my echo's outside the loop [= Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784296 Share on other sites More sharing options...
kittrellbj Posted March 14, 2009 Share Posted March 14, 2009 Hmmm, that is an interesting point. Yes, probably. Not tested, though. Link to comment https://forums.phpfreaks.com/topic/149335-solved-total-up-money/#findComment-784297 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.