Jump to content

Adding values of each instance in a while loop


Recommended Posts

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:

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?


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by speckytwat
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.