speckytwat Posted November 5, 2014 Share Posted November 5, 2014 Hope someone can help, I am trying to add up all the values of a variable in a PHP while loop and have tried several approaches but none seem to work. Hopefully someone can point out what the correct method is.I've set up some code to pull the pay rate from a MySQL table, and then calculate that person's earnings based on the amount of time they spend on a particular job. That works fine: while ($row = $getrate->fetch_assoc()) { $staffrate = $row["Rate"]; $earnings = ($staffrate/3600) * $jobeventtimeseconds; $earningsformatted = number_format($earnings, 2); } However there may be a number of jobs for different people with different rates, so I want to add all of these up and output the total. So for example if I had values of 125.00, 35.50 and 22.75 I want to show 183.25 as the total.So I tried a few things I found on forums on the subject: $totalearnings = 0; foreach($earningsformatted as $key => $value) { $totalearnings += $value; } echo $totalearnings; That didn't work so I tried another method that someone suggested: $total=array($earningsformatted); echo array_sum($total); The 2nd method outputted just the last individual value, the 1st method didn't work at all. Does anyone have a working method that can be used to do this?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/ Share on other sites More sharing options...
requinix Posted November 5, 2014 Share Posted November 5, 2014 There are a few problems but mostly it's $earningsformatted = number_format($earnings, 2);You overwrite $earningsformatted every time. You're doing a database query to get these numbers, right? Right. If you want the total then you should make that query return to you the total. So you don't have to do it yourself. If you're not sure how then we'll need to see the query to tell you how it should change. Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/#findComment-1495768 Share on other sites More sharing options...
speckytwat Posted November 5, 2014 Author Share Posted November 5, 2014 (edited) Hi, this is the query that pulls through the data: $getrate = $mysqli->query("SELECT * FROM rates WHERE StaffName LIKE '$category'"); The $category value is already specified as the name of the person, so the query grabs the data for that person (including the pay rate) from the table. Edited November 5, 2014 by speckytwat Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/#findComment-1495771 Share on other sites More sharing options...
requinix Posted November 5, 2014 Share Posted November 5, 2014 That one person. I thought you were trying to change the code to handle multiple jobs from multiple people? Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/#findComment-1495772 Share on other sites More sharing options...
speckytwat Posted November 5, 2014 Author Share Posted November 5, 2014 Yes, basically the query grabs all the jobs for a particular project. So Job 1 might be done by Person 1 and Job 2 might be done by person 2, and in each case the rates would be different. What I want to do is total up the various earnings for each of these jobs. So for example, let's say the query finds two jobs for the project. Job 1 is 1 hour and done by Person 1 who has a rate of 40, so therefore their earnings = 40. Job 2 is 1.5 hrs and done by Person 2 and their rate is 30, so the earnings = 45. So all I want to do is add together these calculated values, irrspective of person or rate, so in this case it would be 40 plus 45 = 85. Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/#findComment-1495775 Share on other sites More sharing options...
speckytwat Posted November 5, 2014 Author Share Posted November 5, 2014 Anyone have any ideas about how this can be achieved? Quote Link to comment https://forums.phpfreaks.com/topic/292283-adding-values-of-each-instance-in-a-while-loop/#findComment-1495798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.