I have written a function which creates games for all the teams in a database for the current tournament. Teams can meet and play against each other any number of times (user specifies this earlier) then the script puts the teams as home or away.
A tournament can have many groups with teams in those groups. Teams should only play against other teams in the same group.
<?php
function creategames($tourid)
{
$teams = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' ORDER BY group_id DESC")or die(mysql_error());
$i=0;
while ($data = mysql_fetch_array($teams))
{
$groupnmbr[$i] = $data['group_id'];
$i++;
}
$groups = $groupnmbr[0];
$nmbrteams = $i;
$teamspergroup = ($nmbrteams/$groups);
$meettimes = mysql_result(mysql_query("SELECT meetcount FROM tournament WHERE id = '$tourid'"),0)or die(mysql_error());
$gamespergroup = nCr($teamspergroup, $meettimes);
$gamesperteam = (($teamspergroup-1)*$meettimes);
$z=$groups;
while ($z > 0)
{
$groupid = $z;
$teams2 = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' AND group_id = '$groupid' ORDER BY RAND()")or die(mysql_error());
while ($data2 = mysql_fetch_array($teams2)) {
$team1 = $data2['team_id'];
$games = 0;
$games1 = mysql_query("SELECT * FROM games WHERE team_home = '$team1' AND tour_id = '$tourid'")or die(mysql_error());
$games2 = mysql_query("SELECT * FROM games WHERE team_away = '$team1' AND tour_id = '$tourid'")or die(mysql_error());
$game1 = mysql_num_rows($games1);
$game2 = mysql_num_rows($games2);
$games = $game1+$game2;
if ($games < $gamesperteam) {
$teams3 = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' AND group_id = '$groupid' AND team_id != '$team1' ORDER BY RAND()")or die(mysql_error());
while ($insert = mysql_fetch_array($teams3))
{
$team2 = $insert['team_id'];
$gamechecks = mysql_query("SELECT * FROM games WHERE team_home = '$team1' AND team_away = '$team2' AND tour_id = '$tourid'")or die(mysql_error());
$gamecheck = mysql_num_rows($gamechecks);
while ($gamecheck < $meettimes) {
if ($gamecheck % 2) {
mysql_query("INSERT INTO games (tour_id, team_home, team_away) VALUES ('$tourid', '$team1', '$team2')")or die(mysql_error());
}
else
{
mysql_query("INSERT INTO games (tour_id, team_away, team_home) VALUES ('$tourid', '$team1', '$team2')")or die(mysql_error());
}
$gamecheck++;
}
}
}
}
$z--;
}
return 'Games created';
}
?>
This works fine but my problem is that all the first games are mostly for the same team, I was wondering how I could create a list of games where it would for example, when there are 6 teams in total, have team1 and team2 in game 1 then the next game would be a game where team1 and team2 or not playing, maybe team3 and team5. Then the next game would need to be team4 and team6, after this it would start over and find the next games if there are any.
Any ideas on how to solve this? Using rand() when selecting the games from the database sort of solved it, but games with the same team are bound to show next to each other at one time or another.
Updated:
This is what I currently use to select games and display them.
<?php
$games = mysql_query("SELECT * FROM games WHERE tour_id = '$id' AND is_final = '0'");
$gamecount=1;
while ($game = mysql_fetch_array($games)) {
$gameid = $game['id'];
$hometeam = $game['team_home'];
$awayteam = $game['team_away'];
$homescore = $game['home_score'];
$awayscore = $game['away_score'];
$status = $game['status'];
$group = mysql_result(mysql_query("SELECT group_id FROM sessions WHERE tour_id = '$id' AND team_id = '$hometeam'"),0);
$hometeamname = mysql_result(mysql_query("SELECT name FROM teams WHERE id = '$hometeam'"),0);
$awayteamname = mysql_result(mysql_query("SELECT name FROM teams WHERE id = '$awayteam'"),0);
if ($status == 0) {
echo '<br/><br/><table><tr><td width="300px"><form method="post" action="#">';
echo 'To be played</td><td width="300px">Grupp '.$group.' Game '.$gameid.'</td></tr>';
echo '<tr><td colspan="2" align="center">'.$hometeamname.' <input type="text" name="y" size="5"> VS '.$awayteamname.' <input type="text" name="z" size="5"></td></tr>';
echo '<tr><td></td><td><input type="hidden" name="game" value="'.$gameid.'"><input type="hidden" name="i" value="'.$hometeam.'"><input type="hidden" name="x" value="'.$awayteam.'"><input type="submit" value="Klar"></form></td></tr></table>';
}
else if ($status == 1) {
echo 'Completed | '.$hometeamname.' '.$homescore.' - '.$awayscore.' '.$awayteamname.' | Grupp '.$group.'<br/>';
}
$gamecount++;
}
?>
This works when I want to display the games, but what can I do to change the order as specified above so that team1 to team6 all get to play a game before the next game is shown in the list.
Thanks!