Good Morning,

I am writing code for a Hockey Pool site and I am working on the Standings page...typical divisional standings page.

My code runs through a loop for the four teams in each division and yields results for each team and outputs fine. Here is my problem...I want to sum the sums of the four teams.

For example, with Games Played it calculates the total GP for each team with the following chunk of code.

// GP
echo "</td><td width='40'align='center' bgcolor='FFFFFF'><font face='arial' size='2' color='000000'>";
$ttl_GP=$row['GP_wk1']+$row['GP_wk2']+$row['GP_wk3']+$row['GP_wk4']+$row['GP_wk5']+$row['GP_wk6']+$row['GP_wk7']+$row['GP_wk8']+$row['GP_wk9']+$row['GP_wk10']+$row['GP_wk11']+$row['GP_wk12']+$row['GP_wk13']+$row['GP_wk14']+$row['GP_wk15']+$row['GP_wk16']+$row['GP_wk17']+$row['GP_wk18']+$row['GP_wk19']+$row['GP_wk20']+$row['GP_wk21']+$row['GP_wk22']+$row['GP_wk23']+$row['GP_wk24']+$row['GP_wk25']+$row['GP_wk26'];
echo $ttl_GP;

This outputs a result of Team 1 - 57, Team 2 - 57, Team 3 - 58, Team 4 - 60.

I would now like to be able to sum these in final row to yield a result of 232.

Here is what I have tried but it echos "0". I think my syntax may be off.

$div_GP=$ttl_GP[$row['Team']]+$ttl_GP[$row['Team']]+$ttl_GP[$row['Team']]+$ttl_GP[$row['Team']];

Any help would be appreciated.

Dani AI

Generated

The loop already computes each team total correctly, but the division total must be accumulated separately. As pointed out, the attempted expression was effectively indexing the same thing repeatedly. The reliable approaches are: 1) keep a running accumulator in PHP while you loop the teams, or 2) let the database compute the division sum. Updating the table with a precomputed total is usually unnecessary and can introduce data inconsistency unless you maintain it with triggers or a cache invalidation strategy (which was considering).

A compact PHP pattern (declare the accumulator before the team loop, add each team total into it) — adapted to whatever column naming you use — looks like this:

$division_total = 0;

while ($row = mysqli_fetch_assoc($result)) {
    $team_total = 0;
    for ($i = 1; $i <= 26; $i++) {
        $col = 'week' . $i; // example column names: week1 .. week26
        $team_total += isset($row[$col]) ? (int) $row[$col] : 0;
    }
    echo $team_total, "\n";
    $division_total += $team_total;
}

echo 'Division total: ' . $division_total;

If you prefer one-query aggregation, do it in SQL and guard against NULLs with COALESCE (or IFNULL):

SELECT SUM(
  COALESCE(week1,0) + COALESCE(week2,0) + ... + COALESCE(week26,0)
) AS division_games
FROM teams
WHERE division = 'YourDivision';

Quick debugging tips: print individual team totals and the running accumulator during the loop; cast values to integers to avoid string concatenation; check for NULL columns. Only store aggregated totals in the database if profiling proves the live calculation is a performance bottleneck — otherwise compute on the fly or cache the result.

Recommended Answers

All 3 Replies

You're adding the same value over and over again $row['Team'] Assuming each team has their own names (Team1, Team2, Team3 etc) then you could try this:

$div_GP=$ttl_GP[$row['Team1']]+$ttl_GP[$row['Team2']]+$ttl_GP[$row['Team3']]+$ttl_GP[$row['Team4']];

All I've done is add numbers to each.

Thanks...I think we are close...but it is not finding a value for each team and thus adding 0+0+0+0 to give 0. I will keep playing around.

I think I will update the database with the summed total and then echo the sum of that column and that will make it work. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.