Hello
i’m going to have teams and distributes it in a schedule which every team play with the other but not to play twice in the day or with the same team twice .
i tried this code and it works ,but i want to show the name of each team not the numbers. this code accepts the number of teams to play and generate the schedule .
<body>
<form id="4" name="4" action="test.php" method="post">
<input type="text" id="teams" name="teams" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
$teams=array ("team1","team2","team3","team4");
$n =$_POST['teams'];
if (!empty($_GET['n']) && ctype_digit($_GET['n'])) {
$n = $_GET['n'];
}
echo '<pre>' . generateRoundRobinPairings($n) . '</pre>';
function generateRoundRobinPairings($num_players) {
//do we have a positive number of players? otherwise default to 4
$num_players = ($num_players > 0) ? (int)$num_players : 0;
//set number of players to even number
$num_players = ($num_players % 2 == 0) ? $num_players : $num_players + 1;
//format for pretty alignment of pairings across rounds
$format = "%0" . ceil(log10($num_players)) . "d";
$pairing = "$format-$format ";
//set the return value
$ret = $num_players . " Player Round Robin:\
-----------------------";
//print the rounds
for ($round = 1; $round < $num_players; $round++) {
$ret .= sprintf("\
Day #$format : ", $round);
$players_done = array();
//print the pairings
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
//select opponent
//$opponents= array("team")
$opponent = $round - $player;
//echo $opponent.'<br>';
$opponent += ($opponent < 0) ? $num_players : 1;
//ensure opponent is not the current player
if ($opponent != $player) {
//choose colours
if ($player % 2 == $opponent % 2) {
if ($player < $opponent) {
//player plays black
$ret .= sprintf($pairing, $opponent, $player);
} else {
//player plays white
// $ret .= sprintf($pairing, $player, $opponent);
}
} else {
if ($player < $opponent) {
//player plays white
$ret .= sprintf($pairing, $player, $opponent);
} else {
//player plays black
$ret .= sprintf($pairing, $opponent, $player);
}
}
//these players are done for this round
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
//print the last pairing (i.e. for the last player)
if ($round % 2 == 0) {
$opponent = ($round + $num_players)/ 2;
//last player plays white
$ret .= sprintf($pairing, $num_players, $opponent);
} else {
$opponent = ($round + 1) / 2;
//last player plays black
$ret .= sprintf($pairing, $opponent, $num_players);
}
}
return $ret;
}
?>