PHP Sports Scheduler
I had need of a sport scheduling script for a game I was developing. The script had to be in PHP and had to produce a round-robin style schedule that ensured every team played every other team an equal number of times. My last requirement was that it also had to alternate Home and Away teams for each game.
Unable to find such a script anywhere online, I decided to develop my own. And now I give it to anyone who is in need of the same thing. My version includes a simple HTML form that allows you to change the number of teams and the number of weeks/rounds to play. For an odd number of teams, it also incorporates BYE weeks.
<html>
<head>
<title>Sports Scheduler</title>
</head>
<body>
<?php
/*
* schedule.php
*
* Copyright 2011 Stephen Chatelain
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
$teamCount = 6;
// weeks = number of rounds/weeks for each team to play each other once
$weeks = ($teamCount % 2) ? $teamCount : ($teamCount - 1);
$rows = array();
$fullSchedule = array(); // Holds entire season schedule
$games = floor($teamCount / 2);
$teamArray = range(1, $teamCount);
$odd = ($teamCount % 2) ? true : false;
function rotate($rows, $odd) {
// rotate elements from one array to other
$buffer = array_pop($rows[1]);
array_push($rows[0], $buffer);
if ($odd) {
$buffer = array_shift($rows[0]);
} else {
// get second element from first array
$buffer = $rows[0][1];
// remove second element from first array
array_splice($rows[0], 1, 1);
}
array_unshift($rows[1], $buffer);
return $rows;
}
// populate two opposing arrays
$firstHalf = ceil($teamCount / 2);
$secondHalf = $teamCount - $firstHalf;
for ($i=0; $i<$firstHalf; $i++) {
$rows[0][] = $teamArray[$i];
}
for ($i=$firstHalf; $i<$teamCount; $i++) {
$rows[1][] = $teamArray[$i];
}
// populate first week
for ($i=0; $i<$firstHalf; $i++) {
$fullSchedule[0][] = $rows[0][$i];
$fullSchedule[0][] = $rows[1][$i];
}
// populate remaining weeks
for ($week = 1; $week < $weeks; $week++) {
$rows = rotate($rows, $odd);
for ($i=0; $i<$firstHalf; $i++) {
$fullSchedule[$week][] = $rows[0][$i];
$fullSchedule[$week][] = $rows[1][$i];
}
}
echo('<h3>Schedule</h3>');
echo('Teams: ' . $teamCount . '<br />');
echo('Weeks: ' . $weeks . '<br />');
echo('<table style="border-collapse: collapse">');
echo('<th>');
for ($i=0; $i<$games; $i++) {
echo('<td>H --- V</td>');
}
echo('</th>');
$visitor = true;
$trips = 0;
for ($week=0; $week<$weeks; $week++) {
echo("<tr>");
echo("<td style='border:1px solid #000; padding:3px'>Week " . ($week + 1) . "</td>");
$pointer = 0;
// Alternate home/visitor for team #1 when even number of teams
if (!$odd) {
$next = $pointer + 1;
if ($fullSchedule[$week][$pointer] == 1 && !$visitor) {
$fullSchedule[$week][$pointer] = $fullSchedule[$week][$next];
$fullSchedule[$week][$next] = 1;
$visitor = true;
} else {
$visitor = false;
}
}
for ($g=0; $g<$games; $g++) {
echo("<td style='border:1px solid #000; padding:3px'>" . $fullSchedule[$week][$pointer++] . " vs. " . $fullSchedule[$week][$pointer++] . "</td>");
}
// Check for team with a BYE week
if ($teamCount % 2) {
echo('<td style="border:1px solid #000; padding:3px">'.$fullSchedule[$week][$pointer++].' BYE</td>');
}
echo("</tr>");
}
echo("</table>");
echo ($odd) ? '<br />Odd number of teams<br />' : '<br />Even number of teams<br />';
?>
<br />
<h3>Create New Schedule</h3>
<form action="" method="post">
How many teams: <input type="text" name="teams" size="4" /><br />
How many rounds: <input type="text" name="rounds" size="4" /><br />
<input type="submit" name="create" value="Create Schedule" />
</form>
</body>
</html>
Archive
By Category...
- General Discussion (3)
- Development, Games (6)
- Development, Business (1)
- Development, Utilities (1)
- Programming, PHP (2)
- Programming, MySQL (0)
- Programming, Javascript (0)
- Design Concepts (0)
- September 2011 (1)
- January 2011 (1)
- April 2010 (1)
- January 2010 (1)
- December 2009 (4)
- November 2009 (2)
- October 2009 (3)
- Complete Post List (13)
