Q: results table in php I am having trouble understanding how a loop I have created works.
I have input fields on another page, which where results are sent to the results table in my database, with team name, team score, opposition score, opposition name.
Now the page where the results are entered mirrors the current contents of the results table so that previously entered scores appear already in the correct input field, and the ones with no socres enetered are at 0.
This page works perfectly in that a score that is edited, replaces the score in the results table, blank ones are left blank etc.
Howwever, I need to use these results to update my league table table in my database.
Currently I have a rather large loop, which (after I have fetched team name, team score, opposition score, opposition name, from the results table), works out how many points to give that team, and the opposing team, plus how many to add to the 'win' column and the 'loss' column etc of my league table.
The problem I have is that it will only ever do one result per team, because on each iteration of the loop, as soon as it finds a matching if statement (ie if team_score>opposition score) it updates the table, before running through the other results to find that teams results.
Because an edited result needs to be treated as an edited result, not an additional result, this is the only way I have been able to find to get even close.
here is a snippet of the code. There are many more if statementswithin the for loop but they are not needed to describe the problem I am having.
$query = $database->query("SELECT team_name, team_score, opposition_score, opposition_name     from results_a");


while ($row = $query->fetch(PDO::FETCH_NUM)) {

  $team[] = ($row[0]);
  $team_score[] = ($row[1]);
  $opposition_score[] = ($row[2]);
  $opposition[] = ($row[3]);
}
$count=count($team);
for ($i = 0; $i < $count; $i++) {

  $team_bonus[$i] = $team_score[$i] / 2;
  $opposition_bonus[] = $opposition_score[$i] / 2;



  //TEAM WINS, NO OPPOSITION BONUS
  else if (($team_score[$i] > $opposition_score[$i]) && ($team_bonus[$i] > $opposition_score[$i])) {

    $team_points[]+=3;
    $team_win[]+=1;
    $team_draw[]+=0;
    $team_loss[]+=0;
    $team_extra[]+=0;
    $opposition_points[]+=0;
    $opp_win[]+=0;
    $opp_draw[]+=0;
    $opp_loss[]+=1;
    $opp_extra[]+=0;
    $team_played[]+=1;
    $opposition_played[]+=1;
  }

  $team_gd[] = $team_score[$i] - $opposition_score[$i];
  $opposition_gd[] = $opposition_score[$i] - $team_score[$i];

  //UPDATE LEAGUE TABLE
  $query = $database->query("UPDATE pool_a SET played='$team_played[$i]',
     win='$team_win[$i]', 
     draw='$team_draw[$i]', 
     loss='$team_loss[$i]', 
     goals_for='$team_score[$i]', 
     goals_against='$opposition_score[$i]', 
     goal_difference='$team_gd[$i]', 
     bonus_points='$team_extra[$i]', 
     points='$team_points[$i]' 
     where team_name = '$team[$i]'");

  $query2 = $database->query("UPDATE pool_a SET played='$opposition_played[$i]', 
    win='$opp_win[$i]', 
    draw='$opp_draw[$i]', 
    loss='$opp_loss[$i]', 
    goals_for='$opposition_score[$i]', 
    goals_against='$team_score[$i]', 
    goal_difference='$opposition_gd[$i]', 
    bonus_points='$opp_extra[$i]', 
    points='$opposition_points[$i]' 
    where team_name='$opposition[$i]'");

The+= within the if statement are useless, because instead of going to the next row where the team name is the first teams name, it runs the query, then when that teams name appears again, it replaces the data next time the query runs.
Very stuck, have spent a lot of time on this!
If you need any more info let me know.
Many thanks
