I have a database that contains results of fantasy football games. I have a query that returns the results of the games team X played against the other teams in the league. The query works perfect in the database.
Results are formatted:
Opponent Name: America Enforcers
Wins Losses Draws
1 1 0
Last Game: Week 2 of 2011: 416.6 - 369.8
the query and the php code cycle thru each team that team X has played and brings up team X's record and the results of the last game played.
My problem is that the code stops one team short. So Team X played teams A, B, C, D, E
but the code only returns teams A, B, C, D
E is left off.
I can't for the life of me figure out what silly mistake I made.
<?php
include_once('../other/functions.php');
$con = mysql_connect($hostname, $username, $password)
OR DIE ('Unable to connect to database! Please try again later.');
$db = mysql_select_db($dbname, $con);
$thing = $_GET['thing'];
$query = "select selected.teamname AS selected_team, selected_score.score AS selected_score, week.week, year, home_id, ".
"target_score.score as target_score, target.teamname as targetname, week.ID ".
"from owners as selected ".
"JOIN game_scores AS selected_score ON selected.owner_id = selected_score.team_id ".
"JOIN game_setup ON game_setup.game_id = selected_score.game_id ".
"JOIN game_scores AS target_score ON target_score.game_id = game_setup.game_id AND target_score.team_id != selected_score.team_id ".
"JOIN owners AS target ON target.owner_id = target_score.team_id ".
"JOIN week ON week.week = game_setup.week ".
"WHERE selected.owner_id = $thing ".
"and target.active = 1 ".
"GROUP BY target.teamname, year, week.ID ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$wins=0;
$losses=0;
$draws=0;
$last_target = false;
echo '<h2>' . $row1['selected_team'] . ' vs. Active Teams</h2>';
while ($row = mysql_fetch_assoc($result))
{
if ($last_target['targetname'] != $row['targetname'])
{
if ($last_target)
{
printf('
<table border="1" width="600">
<tr>
<th>Opponent Name:</th>
<th colspan="2">%s</th>
</tr>
<tr>
<th>Wins</th>
<th>Losses</th>
<th>Draws</th>
</tr>
<tr>
<td align="center">%d</td>
<td align="center">%d</td>
<td align="center">%d</td>
</tr>
<tr>
<th>Last Game:</th>
<td colspan="2">%s of %s: %.1f - %.1f</td>
</tr>
</table><br/>',
$last_target['targetname'],
$wins,
$losses,
$draws,
$last_target['week'],
$last_target['year'],
$last_target['selected_score'],
$last_target['target_score']
);
}
$wins = $losses = $draws = 0;
}
if ($row['selected_score'] < $row['target_score'])
++$losses;
elseif ($row['selected_score'] == $row['target_score'])
++$draws;
else
++$wins;
$last_target = $row;
}
?>
Any ideas on how to make the magic elvish be good???
Thank you for your time.