I have a list of players for a sports league. I am trying to take the players and put them into teams based on age group and skill level.
Here is where my problem is. There needs to be three teams created with 20 players per team. The record set is retreived by order of skill level (i.e. highest skill first and so on down).
My logic is this.
Each player gets assigned to the next team: player 1 to team 1, player 2 to team 2 and player 3 to team 3, player 4 to team 1, mplayer 5 to team2 and player 6 to team 3 and so on and so forth.
So the team lists would look like this:
For team 1 I need players 1, 4, 7, 10, etc...
For team 2 I need players 2, 5, 8, 11, etc...
For team 3 I need players 3, 6, 9, 12, etc...
When I display the info on the screen I can see team 1 created properly. But I do not get team 2 or three printed. But if I change the value for $i in the for statement I can see team 2 and 3 respectively.
I am sorry for the long explaination but I wanted to make sure that what I am doing is completely understood.
So to finish this long winded essay. I seem to be able to get the teams I need by manually entering the $i value for the loop, but I need to have this automated so all three teams will be printed to the screen for me to see.
Any and all help is very much appreciated.
Here is the source code:
Code: Select all
<?php
$result = mysql_query('select id, age, playLevel from players where age between 4 and 6 and gender = "M" and active = 1 order by playLevel DESC');
$getTeamSettings = mysql_query('select totalPlayers, playersPerTeam, numberOfTeams from teams where age = "4-6" and gender = "M"') or die (mysql_error());
$setTeamSettings = mysql_fetch_row($getTeamSettings);
echo 'Total Players Registered: '.$setTeamSettings[0].'<br>';
echo 'Total Players Per Team: '.$setTeamSettings[1].'<br>';
echo 'Number of Teams: '.$setTeamSettings[2].'<br><br><br>';
$numLoop = $setTeamSettings[2] * $setTeamSettings[1];
$setLimit = $setTeamSettings[2];
$setCount = 1;
for($i=1; $i<=$setLimit;$i++){
$setTeam = 'Team '.$i;
$setPlayers = '';
$count = 1;
$nextPlayer = $i;
while ($row=mysql_fetch_array($result)) {
if ($numLoop >= $count) {
if ($nextPlayer == $count) {
if ($count == $i) {
$setPlayers = $setTeam. ' = '. $row["id"].',';
}
else {
$setPlayers = $setPlayers.$row["id"].',';
}//end if
//if player was added then increment the count
$nextPlayer = $nextPlayer + $setLimit;
}//end if add player to list
$count++;
}//end if maximum players per team
}//end while
//trying to print the teams to the screen
echo $i.'. '.$setPlayers.'<br>';
}//end for loop
?>