I'm thinking that you have a logic flaw in what you are trying to do, here is how I would do this to make everything work.
- select all games for the given season
- loop over each game
- for each game select the list of referees available
- display each game on my page
Now that logic works but it creates n+1 queries to be processed (n = number of games in the season, 1 = the original query to get the games). Running n+1 queries could get to be very large and time consuming so I'd change this to work using a single query that got me all of my games for a season with all of the referees that are available for those games, and I'd do the processing work in PHP.
SELECT g.date, g.time, g.location, g.home, g.visitor, g.age, a.name
FROM games g
INNER JOIN availability a ON (g.date = a.date)
WHERE g.season = "2005-02"
ORDER BY g.date
Now that query will get me all of my game information plus each referee that is available for that date. But it will get me all of my game information for each referee.
date, time, location, home, visitor, age, name
1/1, 10:00, field1, team1, team2, 18, John Doe
1/1, 10:00, field1, team1, team2, 18, Jane Doe
1/1, 10:00, field1, team1, team2, 18, Frank Smith
1/1, 10:00, field1, team1, team2, 18, Sammy Someone
As you can see there is alot of duplicate data that we will need to deal with, but the advantage is we only ran 1 query. So to process this data I'd do something like this:
$currDate = null; // Placeholder
$gameArray = null; // Another Placeholder
$games = array();
while ($row = mysql_fetch_assoc($res))
{
if ($currDate != $row["date"])
{
$currDate = $row["date"];
if ($gameArray != null)
{
$games[] = $gameArray;
}
$gameArray["date"] = $row["date"];
$gameArray["time"] = $row["time"];
$gameArray["location"] = $row["location"];
$gameArray["home"] = $row["home"];
$gameArray["visitor"] = $row["visitor"];
$gameArray["age"] = $row["age"];
$gameArray["refs"] = array();
}
$gameArray["refs"][] = $row["name"];
}
// Process $games array here to build table/form
Now I've run a single query to get all of my data and I've walked through that data to put it into a form that I can easily use to build my table and form for display to my user.