1

I am wondering for all of you more experienced coders how I would take a value coming from my database, which is stored as a double

(Variables regulartime and overtime)

And then calculate the sum of each of the two columns..

foreach ($result as $row) {
    echo '<tr><td>' . $row['username'] . '</td>
        <td>' . $row['date'] . '</td>
        <td>' . $row['jobnumber'] . '</td>
        <td>' . $row['customer'] . '</td>
        <td>' . $row['worksite'] . '</td>
        <td>' . $row['duties'] . '</td>
        <td>' . $row['regulartime'] . '</td>
        <td>' . $row['overtime'] . '</td></tr>';


}

Thank you so much for your help.

Tyler

EDIT I want to add a final row at the bottom of the two to display the sums

6
  • 1
    $sum = $row['foo'] + $row['bar']? it's just simple addition... Commented May 7, 2015 at 14:40
  • 1
    I would do it in the SQL not in PHP. "SELECT username, date, jobnumber, customer, worksite, duties, regulartime, overtime, (overtime` + regulartime) as totaltime from table;" Commented May 7, 2015 at 14:40
  • You want to add a final row containing the sums? Commented May 7, 2015 at 14:41
  • @MarcB I want to add a final row at the bottom of the two to display the sums Commented May 7, 2015 at 14:42
  • so put an echo "Total: $sum" after the loop finishes. Commented May 7, 2015 at 14:43

3 Answers 3

2

As you want to display a table containing each row, then a final row containing the sum, you only have to use your existing foreach loop to increment the totals :

$totalRegularTime = 0;
$totalOverTime = 0;

foreach ($result as $row) {
    echo '<tr><td>' . $row['username'] . '</td>
        <td>' . $row['date'] . '</td>
        <td>' . $row['jobnumber'] . '</td>
        <td>' . $row['customer'] . '</td>
        <td>' . $row['worksite'] . '</td>
        <td>' . $row['duties'] . '</td>
        <td>' . $row['regulartime'] . '</td>
        <td>' . $row['overtime'] . '</td></tr>';

    $totalRegularTime += $row['regulartime'];
    $totalOverTime += $row['overtime'];
}

echo '<tr><td colspan="6">TOTAL</td>
    <td>' . $totalRegularTime . '</td>
    <td>' . $totalOverTime . '</td></tr>';



However if you didn't need to display all the rows in a table first, the best solution would have been to get the sums using SQL.
Eternal1's answer is a good alternative if you can't use SQL (if your data doesn't come from database for example). And it's pretty clean.

Sign up to request clarification or add additional context in comments.

Comments

2
function calculateSum(array $data, $field)
{
    return array_reduce(
        $data,
        function ($result, $elem) use ($field) {
            return $result + $elem[$field];
        }, 0
    );
}

calculateSum($result, 'regulartime');
calculateSum($result, 'overtime');

Comments

1

I would do it in the SQL not in PHP.

SELECT username, date, jobnumber, customer, worksite, duties, regulartime, overtime, (overtime + regulartime) as totaltime FROM table;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.