Page 1 of 1

Creating a game schedule for sports teams

Posted: Sat May 22, 2004 10:16 am
by Yusuf
Hi,

Here is a short desc. of what I am trying to do.

6 teams. 10 weeks. 1 game/week/team.

Each week on Sat. all teams will play 1 game. During the course of the season each team will have played each other twice.

I have to figure out how to generate the schedule so that the teams pair up each week with no over lap. The schedule will just repeat after week 5.

i.e.

Week 1
1 x 2
3 x 4
5 x 6

Week 2
1 x 3
2 x 5
4 x 6

Week 3
1 x 4
3 x 5
2 x 6


I can get the first week but not any subsequent weeks.

I cannot see a pattern or logic to this (which is why I am posting this question). The season starts in a week so I have to figure this out ASAP.

Any and all help is very appreciated and extremely welcome.

Here is what I have so far:

Code: Select all

<?php

//get the length of the season in weeks
	$result = mysql_query('SELECT TO_DAYS(leagueEndDate)-TO_DAYS(leagueStartDate)as date_diff FROM '.$tblPre.'options') or die (mysql_error()); 
	$getDates = mysql_fetch_row($result);
	$leagueLength = round(($getDates[0] / 7) + 1);


//get start end end season dates
	$getGameDates = mysql_query('SELECT leagueEndDate, leagueStartDate FROM '.$tblPre.'options') or die (mysql_error()); 
	$setGameDates = mysql_fetch_row($getGameDates);


$age = "4-6"
$gender = "M"

$gen = "Boys";

//get field info
	$getField = mysql_query('select f.fieldName, f.fieldNo, a.age, a.numberOfTeams from '.$tblPre.'fields f, '.$tblPre.'maxages a where f.division = a.id and f.gender = "'.$gender.'" and a.age = "'.$age.'"') or die (mysql_error());
	$getField = mysql_fetch_row($getField);
	

//get teams
	$getTeams = mysql_query('select sponsor, teamNo from '.$tblPre.'teams where gender = "'.$gender.'" and ageGroup = "'.$age.'"') or die (mysql_error());



$firstDate = substr($setGameDates[1],8,2).'/'.substr($setGameDates[1],5,2).'/'.substr($setGameDates[1],0,4);
$lastDate = substr($setGameDates[0],8,2).'/'.substr($setGameDates[0],5,2).'/'.substr($setGameDates[0],0,4);

$x = explode('/', $firstDate);
$firstDate = $x[2].'-'.$x[1].'-'.$x[0];
$x = explode('/', $lastDate);
$lastDate = $x[2].'-'.$x[1].'-'.$x[0];

$firstTimeStamp = strtotime($firstDate);
$lastTimeStamp = strtotime($lastDate);


echo '
<table width="100%">
	<tr>
		<td width="25%"><b>Division: '.$getField[2].' = '.$getField[3].' Teams</b></td>
		<td align="center" width="50%"><h3>Field Name: '.$getField[0].'</h3></td>
		<td width="25%"><b>Field Number: '.$getField[1].'</b></td>
	</tr>
</table>
<br>
<table width="95%" align="center" cellspacing="0">
		<tr bgColor="#4167B2">
';

$setRow = 1;
for ($i=$firstTimeStamp; $i<=$lastTimeStamp; $i=$i+(24*60*60*7)){
		echo '
			<td style="border-top: 1px solid #000000; border-right: 1px solid #000000;'; if($setRow == 1){ echo 'border-left: 1px solid #000000;';} echo '" align="center"><font color="#FFFFFF"><b>'.date("F d",$i).'</b></font></td>		
		';		
$setRow++;
}

echo '</tr><tr bgColor="#55A0FF">';

$numWeek = 1;
$setRow1 = 1;
for ($i=$firstTimeStamp; $i<=$lastTimeStamp; $i=$i+(24*60*60*7)){
		echo '
		<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;'; if($setRow1 == 1){ echo 'border-left: 1px solid #000000;';} echo '" align="center"><b>Week '.$numWeek.'</b></td>';
	$numWeek++;
	$setRow1++;
}

	echo '
	</tr>
	<tr>
	';

$setRow2 = 1;
for ($i=$firstTimeStamp; $i<=$lastTimeStamp; $i=$i+(24*60*60*7)){
	echo '
	<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;'; if($setRow2 == 1){ echo 'border-left: 1px solid #000000;';} echo '" align="center">
	';
//get number of loops
	$numLoops = $getField[3]/2;
	for($x=1;$x<=$numLoops;$x++){

		if ($x == 1) {
			$op1 = $x;
			$op2 = $x + 1;	
		}
		else {
			$op1 = $x + $x - 1;
			$op2 = $x + $x;
		}
		
		echo $op1.' vs. '.$op2.'<br>';

	}
$setRow2++;
}
	echo '
	</td>
</tr>
</table>

	';



?>