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??

## 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, learning, and sharing knowledge.