I need to generate sequences of games using the round robin algorithm. I have the php page where the user can input the tournament name which will be inserted into the database and it has got a drop down menu up to 32 teams (select number of teams).

So if I select 4 teams in the page, so it will be from team 1 to team 4 which would be 6 matches because every team plays the other team once. I know how the algorithm works but I am not quite sure how to write the query for that.

I created the table team:

Team_id Teams
01 Team 1
02 Team 2
03 Team 3
etc etc

And a bit of php:

var n = teams; //entered teams
var nr = n-1; //Rounds


$result = mysql_query("SELECT teams from team");

while($row = mysql_fetch_array($result))

{

for(i=1; i<n; i++)


print "{$row} <br>";
}

Can anyone help me to do the rotatation of the arrays to create the round robin, please??

Dani AI

Generated

If you want actual rounds (not just all pair combinations), use the classic "circle method." Unlike the nested-pair approach shared, this groups fixtures by week and handles odd team counts with a bye. Also, switch from the old mysql_* API in your snippet: it was removed in PHP 7; use PDO or mysqli.

Example using PDO and a simple matches table (home/away balanced and optional double round robin). It loads team_id values, builds the schedule, then inserts one row per match per round.

// schema idea:
// CREATE TABLE matches (id INT AUTO_INCREMENT PRIMARY KEY,
//   tournament_id INT NOT NULL, round_no INT NOT NULL,
//   home_team_id INT NOT NULL, away_team_id INT NOT NULL);

function buildRoundRobin(array $teams, bool $double = false): array {
    $teams = array_values($teams);
    if (count($teams) % 2 === 1) $teams[] = null; // bye
    $n = count($teams);
    $rounds = $n - 1;
    $schedule = [];

    for ($r = 0; $r < $rounds; $r++) {
        $round = [];
        for ($i = 0; $i < $n / 2; $i++) {
            $a = $teams[$i];
            $b = $teams[$n - 1 - $i];
            if ($a !== null && $b !== null) {
                // flip home/away on odd rounds for balance
                $round[] = ($r % 2 === 0) ? [$a, $b] : [$b, $a];
            }
        }
        $schedule[] = $round;

        // rotate all but the first team
        $fixed = $teams[0];
        $rest  = array_slice($teams, 1);
        array_unshift($rest, array_pop($rest));
        $teams = array_merge([$fixed], $rest);
    }

    if ($double) {
        foreach ($schedule as $round) {
            $schedule[] = array_map(fn($m) => [$m[1], $m[0]], $round);
        }
    }
    return $schedule;
}

// usage
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','user','pass');
$tournamentId = 1;

$teams = $pdo->query('SELECT team_id FROM team ORDER BY team_id')
             ->fetchAll(PDO::FETCH_COLUMN);

$schedule = buildRoundRobin($teams, false);

$ins = $pdo->prepare(
    'INSERT INTO matches (tournament_id, round_no, home_team_id, away_team_id)
     VALUES (?,?,?,?)'
);

foreach ($schedule as $roundNo => $matches) {
    foreach ($matches as [$home, $away]) {
        $ins->execute([$tournamentId, $roundNo + 1, $home, $away]);
    }
}

Tip: If you get an odd team count, the null is a bye; skip inserting those. For , single-elimination is a different generator (bracket/seed tree), not round robin. For , you can map each $round to a date before inserting.

Recommended Answers

All 5 Replies

Hi Sivapatham and welcome to DaniWeb,

A very simple algorithm for this would be:

for( $i = 0; $i < $n; $i++ ) // if $n is number of teams
{
  for( $j = $n-1; $j > $i; $j-- )
  {
    // teams for this match have id's = $i and $j
    // I'll let you work out what you want to do here...
  }
}

I am sure there is a more efficient way of doing this, but this algorithm should work.

If someone is still looking for a simple round robin php script then leave a note. I just created a new script from scratch that does just that. Send any array of information to my function and it will generate the schedule for you. You can then attach every created round to a date or whatever you want to do with it.

I need a script like this. A MySQL that have got the teams. Then a script should make a trounament tree. Can someone help me?

Ok, sry. I need a Single Elimination PHP + MYSQL Script. No Round Robin.

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.