chriscloyd Posted July 19, 2014 Share Posted July 19, 2014 I am having a dilemma. I am working on a script for a client. It is a match making system. It takes all the teams, minimum of 20 teams. 10 matches a season. No team can play the same team in the season. Here is my code I have so far. If it does not make sense ask me, I will try to explain more in depth. <?php error_reporting(E_ALL); include("security/config.php"); //create season for testing purposes $sql_season = "insert into season (`season`) values ('1')"; $conn->query($sql_season) or die($conn->error); $season = 1; //testing purpose echo "Season 1:<br />"; //testing purpose //get number of teams $sql_teams = "select * from teams"; $rs = $conn->query($sql_teams) or die($conn->error); $num_of_teams = $rs->num_rows; echo " Number of teams this season: {$num_of_teams}<br />"; //testing purpose //put all teams in array $teams = array(); while($ts = $rs->fetch_array(MYSQLI_ASSOC)){ $teams[] = $ts; } if($num_of_teams % 2 != 0){ $teams[] = array('name' => 'bye week', 'id' => 0); $sql_dummy = "insert into teams (`name`) values ('bye week')"; $conn->query($sql_dummy); } //how many matches $weeks = 10; $matches = 10; echo " This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />"; //testing purpose for($i = 0; $i < $weeks; $i++){ //mix up teams $teams1 = $teams; $teams2 = $teams; shuffle($teams1); shuffle($teams2); $week = $i + 1; //$sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')"; //$conn->query($sql_week) or die($conn->error); //$weekID = $conn->insert_id; echo " Week {$week} Created:<br />"; //$matchup = array(); for($x = 0; $x < $matches; $x++){ shuffle($teams1); shuffle($teams2); $team_1 = array_pop($teams1); $team_2 = array_pop($teams2); $rs = false; $rs = ($team_1['id'] == $team_2['id'] ? true : false); $sql_search = "select * from matches where season_id = '{$season}' " $sql_search .= "and team_1 = ('{$team_1['id']}' or '{$team_2['id']}') "; $sql_search .= "and team_2 = ('{$team_1['id']}' or '{$team_2['id']}')"; $rss = $conn->query($sql_search) or die($conn->error); if(!$rs || !$rss){ $sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$week}', '{$season}')"; $rs = $conn->query($sql_create); if($rs){ echo " "; //testing purpose echo "{$team_1['name']} vs {$team_2['name']}<br />"; //testing purpose } else{ echo 'error: ' . $conn->error . '<br />'; //testing purpose } } else{ $teams1[] = $team_1; $teams2[] = $team_2; $x--; } } } Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted July 19, 2014 Author Share Posted July 19, 2014 I noticed i had some errors in my code. Updated code and still no luck <?php error_reporting(E_ALL); include("security/config.php"); //create season for testing purposes $sql_season = "insert into season (`season`) values ('1')"; $conn->query($sql_season) or die($conn->error); $season = 1; echo "Season 1:<br />"; //get number of teams $sql_teams = "select * from teams"; $rs = $conn->query($sql_teams) or die($conn->error); $num_of_teams = $rs->num_rows; echo " Number of teams this season: {$num_of_teams}<br />"; //put all teams in array $teams = array(); while($ts = $rs->fetch_array(MYSQLI_ASSOC)){ $teams[] = $ts; } if($num_of_teams % 2 != 0){ $teams[] = array('name' => 'bye week', 'id' => 0); $sql_dummy = "insert into teams (`name`) values ('bye week')"; $conn->query($sql_dummy); } //how many matches $weeks = 10; $matches = 10; echo " This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />"; for($i = 0; $i < $weeks; $i++){ //mix up teams $teams1 = $teams; $teams2 = $teams; shuffle($teams1); shuffle($teams2); $week = $i + 1; $sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')"; $conn->query($sql_week) or die($conn->error); //$weekID = $conn->insert_id; echo " Week {$week} Created:<br />"; $matchup = array(); for($x = 0; $x < $matches; $x++){ shuffle($teams1); shuffle($teams2); $team_1 = array_pop($teams1); $team_2 = array_pop($teams2); $rs = true; $rs = ($team_1['id'] != $team_2['id'] ? true : false); $sql_search = "select * from matches where season_id = '{$season}' "; $sql_search .= "and team_1 = ('{$team_1['id']}' or '{$team_2['id']}') "; $sql_search .= "and team_2 = ('{$team_1['id']}' or '{$team_2['id']}')"; $rss = $conn->query($sql_search) or die($conn->error); if($rs && !$rss){ $sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$week}', '{$season}')"; $rs = $conn->query($sql_create); if($rs){ echo " "; echo "{$team_1['name']} vs {$team_2['name']}<br />"; } else{ echo 'error: ' . $conn->error . '<br />'; } } else{ $teams1[] = $team_1; $teams2[] = $team_2; $x--; } } } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 19, 2014 Share Posted July 19, 2014 the way this is normally done is to have your teams in an array, make a copy of that array, rotate the second array, taking the end entry and putting it back into the start, match it up with the original team array, ignoring the match-up between the identical team in both arrays. if i can find an example among my archived code, i will post it. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted July 19, 2014 Author Share Posted July 19, 2014 thanks, i made it work Data printed: Season 1: Number of teams this season: 42 This Seaon will be 10 consisting of 10 matches Week 1 Created: Team 12 vs Team 31 Team 3 vs Team 40 Team 8 vs Team 6 Team 23 vs Team 5 Team 25 vs Team 1 Team 18 vs Team 39 Team 9 vs Team 14 Team 21 vs Team 15 Team 36 vs Team 38 Team 17 vs Team 7 Team 19 vs Team 11 Team 16 vs Team 20 Team 29 vs Team 32 bye week vs Team 2 Team 22 vs Team 10 Team 27 vs Team 13 Team 24 vs Team 34 Team 4 vs Team 35 Team 33 vs Team 37 Team 28 vs Team 20 Team 26 vs Team 30 Week 2 Created: Team 10 vs Team 23 Team 27 vs Team 29 Team 1 vs Team 13 Team 40 vs Team 34 Team 35 vs Team 24 Team 37 vs Team 2 Team 25 vs Team 39 Team 20 vs Team 32Team 36 vs Team 38 ==== Played already find new match up Team 28 vs Team 7 Team 33 vs Team 31 Team 16 vs Team 11 Team 19 vs Team 8 Team 20 vs Team 9 Team 6 vs Team 14 Team 12 vs Team 26 Team 5 vs Team 22 Team 4 vs Team 30 Team 17 vs Team 36 bye week vs Team 21 Team 18 vs Team 3 Team 38 vs Team 15 Week 3 Created: Team 16 vs Team 18 bye week vs Team 32 Team 2 vs Team 11 Team 23 vs Team 29 Team 20 vs Team 36 Team 21 vs Team 10 Team 9 vs Team 24 Team 13 vs Team 6 Team 7 vs Team 26 Team 40 vs Team 39 Team 33 vs Team 12 Team 8 vs Team 3 Team 38 vs Team 27 Team 34 vs Team 14 Team 4 vs Team 31 Team 22 vs Team 19 Team 5 vs Team 30 Team 1 vs Team 20 Team 28 vs Team 17 Team 15 vs Team 37 Team 25 vs Team 35 Week 4 Created: Team 20 vs Team 11 Team 27 vs Team 28 bye week vs Team 15 Team 17 vs Team 2 Team 9 vs Team 21 Team 16 vs Team 13 Team 39 vs Team 5 Team 24 vs Team 6 Team 32 vs Team 19 Team 22 vs Team 37 Team 26 vs Team 10 Team 4 vs Team 36 Team 23 vs Team 20 Team 8 vs Team 34 Team 18 vs Team 14 Team 12 vs Team 1 Team 31 vs Team 7 Team 25 vs Team 40 Team 30 vs Team 33 Team 29 vs Team 35 Team 38 vs Team 3 Week 5 Created: Team 18 vs Team 8 Team 22 vs Team 35 Team 19 vs Team 39 Team 12 vs Team 40Team 37 vs Team 33 ==== Played already find new match up Team 6 vs Team 20 Team 24 vs Team 5Team 32 vs Team 20 ==== Played already find new match up Team 3 vs Team 32Team 20 vs Team 1 ==== Played already find new match upbye week vs Team 15 ==== Played already find new match upTeam 13 vs Team 16 ==== Played already find new match up Team 7 vs Team 13 Team 4 vs Team 2 Team 30 vs Team 11 Team 1 vs Team 10 Team 29 vs Team 38Team 15 vs Team 21 ==== Played already find new match up Team 27 vs Team 20 Team 28 vs Team 37 Team 25 vs Team 34 Team 9 vs bye weekTeam 21 vs Team 15 ==== Played already find new match up Team 33 vs Team 26 Team 16 vs Team 31 Team 36 vs Team 21 Team 15 vs Team 14 Team 23 vs Team 17 Week 6 Created: Team 14 vs Team 36 Team 7 vs Team 40 Team 20 vs Team 30 Team 24 vs Team 4Team 28 vs Team 37 ==== Played already find new match up Team 13 vs Team 22Team 39 vs Team 5 ==== Played already find new match up bye week vs Team 33 Team 8 vs Team 10 Team 25 vs Team 18 Team 12 vs Team 27 Team 21 vs Team 28 Team 38 vs Team 9 Team 5 vs Team 32 Team 23 vs Team 16 Team 34 vs Team 20 Team 17 vs Team 3 Team 11 vs Team 1 Team 37 vs Team 31 Team 26 vs Team 2 Team 19 vs Team 6 Team 29 vs Team 15 Team 35 vs Team 39 Week 7 Created: Team 11 vs Team 17 Team 4 vs Team 22 Team 39 vs Team 1 Team 20 vs Team 24 Team 33 vs Team 7Team 12 vs Team 26 ==== Played already find new match up Team 6 vs Team 35 Team 2 vs Team 36 Team 37 vs Team 19 Team 5 vs Team 14 Team 38 vs Team 10 Team 28 vs Team 3 Team 40 vs Team 32 Team 21 vs Team 23 Team 15 vs Team 20 Team 26 vs Team 27 Team 8 vs Team 9 Team 16 vs Team 34 Team 29 vs Team 31 Team 25 vs bye week Team 18 vs Team 12 Team 30 vs Team 13 Week 8 Created:Team 26 vs Team 33 ==== Played already find new match up Team 40 vs Team 20 Team 25 vs Team 26 Team 10 vs Team 2 Team 35 vs Team 5 Team 30 vs Team 14 Team 15 vs Team 7Team 20 vs Team 24 ==== Played already find new match up Team 33 vs Team 27 Team 9 vs Team 20Team 8 vs Team 6 ==== Played already find new match upTeam 29 vs Team 23 ==== Played already find new match up Team 23 vs Team 19 Team 31 vs Team 18 Team 4 vs bye week Team 11 vs Team 29 Team 1 vs Team 32 Team 6 vs Team 22 Team 16 vs Team 37 Team 12 vs Team 28 Team 36 vs Team 3 Team 38 vs Team 34 Team 24 vs Team 8 Team 17 vs Team 39 Team 21 vs Team 13 Week 9 Created: Team 2 vs Team 20Team 31 vs Team 33 ==== Played already find new match up Team 21 vs Team 22 Team 17 vs Team 27Team 12 vs Team 26 ==== Played already find new match up Team 39 vs Team 16 Team 11 vs Team 25 Team 35 vs Team 19 Team 13 vs Team 32Team 34 vs Team 20 ==== Played already find new match up Team 6 vs Team 18Team 7 vs Team 28 ==== Played already find new match up Team 28 vs Team 4 Team 36 vs Team 8Team 33 vs Team 31 ==== Played already find new match up Team 30 vs Team 37 Team 40 vs Team 9 Team 26 vs bye week Team 24 vs Team 12Team 14 vs Team 34 ==== Played already find new match up Team 34 vs Team 7 Team 29 vs Team 10 Team 38 vs Team 14 Team 15 vs Team 5Team 20 vs Team 23 ==== Played already find new match up Team 31 vs Team 3 Team 1 vs Team 20 Team 23 vs Team 33 Week 10 Created: Team 38 vs Team 20 Team 40 vs Team 30 Team 34 vs Team 28Team 20 vs Team 1 ==== Played already find new match upTeam 22 vs Team 10 ==== Played already find new match up Team 25 vs Team 24 Team 29 vs Team 20Team 19 vs Team 11 ==== Played already find new match up Team 1 vs Team 4 Team 37 vs Team 36 Team 7 vs Team 2 bye week vs Team 18Team 14 vs Team 6 ==== Played already find new match upTeam 9 vs Team 21 ==== Played already find new match up Team 21 vs Team 31 Team 5 vs Team 26Team 23 vs Team 19 ==== Played already find new match up Team 10 vs Team 33Team 6 vs Team 14 ==== Played already find new match up Team 17 vs Team 6Team 35 vs Team 22 ==== Played already find new match up Team 39 vs Team 23 Team 32 vs Team 12 Team 22 vs Team 8 Team 14 vs Team 3Team 27 vs Team 13 ==== Played already find new match up Team 16 vs Team 35 Team 13 vs Team 15 Team 11 vs Team 9 Team 19 vs Team 27 Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted July 19, 2014 Author Share Posted July 19, 2014 my code <?php error_reporting(E_ALL); include("security/config.php"); //create season for testing purposes $sql_season = "insert into season (`season`) values ('1')"; $conn->query($sql_season) or die($conn->error); $season = 1; //echo "Season 1:<br />"; //get number of teams $sql_teams = "select * from teams"; $rs = $conn->query($sql_teams) or die($conn->error); $num_of_teams = $rs->num_rows; //echo " Number of teams this season: {$num_of_teams}<br />"; //put all teams in array $teams = array(); while($ts = $rs->fetch_array(MYSQLI_ASSOC)){ $teams[] = $ts; } if($num_of_teams % 2 != 0){ $teams[] = array('name' => 'bye week', 'id' => 0); $sql_dummy = "insert into teams (`name`) values ('bye week')"; $conn->query($sql_dummy); } //how many matches $weeks = 10; $matches = 10; //echo " This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />"; for($i = 0; $i < $weeks; $i++){ //mix up teams $teams1 = $teams; shuffle($teams1); $number_of_matches = (count($teams) / 2); shuffle($teams); $teams1 = array_slice($teams, 0, $number_of_matches); $teams2 = array_slice($teams, $number_of_matches, $number_of_matches); $teams2 = array_reverse($teams2); shuffle($teams1); shuffle($teams2); $week = $i + 1; $sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')"; $conn->query($sql_week) or die($conn->error); $weekID = $conn->insert_id; //echo " Week {$week} Created:<br />"; $matchup = array(); $x = 0; do{ shuffle($teams1); shuffle($teams2); $team_1 = array_pop($teams1); $team_2 = array_pop($teams2); $sql_search = "select * from matches where (`team_1` = '{$team_1['id']}' or `team_1` = '{$team_2['id']}') and (`team_2` = '{$team_1['id']}' or `team_2` = '{$team_2['id']}') and `season_id` = '{$season}'"; $rs = $conn->query($sql_search) or die($conn->error); if($rs->num_rows == 0){ $sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$weekID}', '{$season}')"; $rs = $conn->query($sql_create); $x++; } else{ //echo "{$team_1['name']} vs {$team_2['name']} ==== Played already find new match up<br />"; $teams2[] = $team_1; $teams1[] = $team_2; array_reverse($teams2); shuffle($teams1); } } while($x < $number_of_matches); } Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted July 19, 2014 Solution Share Posted July 19, 2014 if that's the code that produced the above schedule, team 20 is going to be very tired by the end of the season as they are playing two games each week all season long. here's an example of the array shift method (add enough 'bye' entries to make the array an even number of entries, plus groups of two 'bye' entries if you want to reduce the number of games in any week, to make the season longer) - <?php $num_teams = 41; $teams = range(1,$num_teams); if(count($teams) % 2){ $teams[] = 'bye'; // make even } $numteams = sizeof($teams); $team_list = array_keys($teams); $tmp_list = array_splice($team_list, 1, sizeof($team_list) - 1); $count = sizeof($tmp_list) + 1; $days = array(); for ($i = 0;$i < $numteams - 1;$i++) { $days[$i] = array(); $day_list = array_merge(array($team_list[0]), $tmp_list); for ($j = 0;$j < $count / 2;$j++) { $days[$i][] = $teams[$day_list[$j]] . ' vs ' . $teams[$day_list[$count - $j - 1]]; } // rotate $tmp_list $elm = array_shift($tmp_list); array_push($tmp_list, $elm); } echo "<pre>"; print_r($days); echo "</pre>"; ?> btw - if you want every team to play every other team at least once, it will take many more weeks then 10 for 42 teams (you actually only show 40 teams in the result.) and because the bye's, if you have more than one, would be grouped if added to the end of the team array, you would need to randomize the $days array in the above code so that teams don't sit out multiple weeks in a row. edit: if you have more than one 'bye', it would be better to just evenly distribute them in the original array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.