Jump to content

automated matching


chriscloyd

Recommended Posts

no im trying ill show u my code

 

<?php
$get_allteams = mysql_query("SELECT * FROM teams WHERE game = 'css' AND division = 'a'");
$num_teams = mysql_num_rows($get_allteams);
$max_teams = $num_teams;
$min_teams = 1;
while ($teams = mysql_fetch_assoc($get_allteams)) {
     $get_home_team = rand($min_teams,$max_teams);
     $get_away_team = rand($min_teams,$max_teams);
}
?>

 

but thats all i got i dont know what to do if they are the same and how to go back and choose a new number or to check if they have already been matched up previous matches or if they have already been matched up to a dif team this week

Link to comment
Share on other sites

Well you need to store the matches in an array, particularly a multi-dim array I would say. So here is the logic for the below. First put all teams into an array. Next do a loop until we tell it to continue, and as long as there are more than 2 teams in the teams array this will continue.

 

First get a random index, use that index to reference a team in the array, once that is done remove that team from the array. Rinse and repeat. We should now have 2 teams let's match them up in the team matches array. Once that is done keep repeating until there are no more or not enough teams to matchup.

 

<?php
$get_allteams = mysql_query("SELECT * FROM teams WHERE game = 'css' AND division = 'a'");
$num_teams = mysql_num_rows($get_allteams);
$max_teams = $num_teams;
$min_teams = 1;
$team_matches = array();

// first setup a teams array
$i=0;
while ($team = mysql_fetch_assoc($get_allteams)) {
     $teams[$i++] = $team;
}

$continue = false;
$i=0;
while (!$continue) {
    // if there are less than 2 teams in the array, do not enter if.
    if (count($teams) < 2) {
        $index = rand(0,count($teams))
        $get_home_team = $teams[$index];

        unset($teams[$index]); // remove the team from the list

        $index = rand(0,count($teams))
        $get_away_team = $teams[$index];
        unset($teams[$index]); 

        $team_matches[$i]['away'] = $get_away_team;
        $team_matches[$i++]['home'] = $get_home_team;
    }else {
         $continue = true;
    }
}

// lets prnt out a test
print_r($team_matches);
?>

 

Should get you what you want.

Link to comment
Share on other sites

My logic was wrong on the < 2 part. It should be >= 2. Try this.

 

<?php
$get_allteams = mysql_query("SELECT * FROM teams WHERE game = 'css' AND division = 'a'");
$team_matches = array();

// first setup a teams array
$i=0;
while ($team = mysql_fetch_assoc($get_allteams)) {
     $teams[$i++] = $team;
}

$continue = false;
$i=0;
while (!$continue) {
    // if there are less than 2 teams in the array, do not enter if.
    if (count($teams) >= 2) {
        $index = rand(0,count($teams))
        $get_home_team = $teams[$index];

        unset($teams[$index]); // remove the team from the list

        $index = rand(0,count($teams))
        $get_away_team = $teams[$index];
        unset($teams[$index]); 

        $team_matches[$i]['away'] = $get_away_team;
        $team_matches[$i++]['home'] = $get_home_team;
    }else {
         $continue = true;
    }
}

// lets prnt out a test
print_r($team_matches);
?>

 

 

Link to comment
Share on other sites

sry actually i have this now

 

Array ( [0] => Array ( [away] => [home] => Array ( [tid] => 2 [teamname] => 2 [irc] => [website] => [tag] => [bio] => [picture] => [wins] => 0 [draws] => 0 [losses] => 0 [manager] => [leader] => [scheduler] => [roundswon] => 0 [roundslost] => 0 [password] => [location] => [game] => cs16 [division] => a ) ) [1] => Array ( [away] => [home] => Array ( [tid] => 3 [teamname] => 3 [irc] => [website] => [tag] => [bio] => [picture] => [wins] => 0 [draws] => 0 [losses] => 0 [manager] => [leader] => [scheduler] => [roundswon] => 0 [roundslost] => 0 [password] => [location] => [game] => cs16 [division] => a ) ) [2] => Array ( [away] => [home] => ) [3] => Array ( [away] => [home] => ) [4] => Array ( [away] => Array ( [tid] => 1 [teamname] => unleash the beast inside [irc] => none [website] => none [tag] => utbi [bio] => [picture] => [wins] => 0 [draws] => 0 [losses] => 0 [manager] => pro[L]ax [leader] => pro[L]ax [scheduler] => pro[L]ax [roundswon] => 0 [roundslost] => 0 [password] => ****** [location] => Pacific [game] => cs16 [division] => a ) [home] => ) )

Link to comment
Share on other sites

Alright I did some testing and I forgot to re-sort the array.

 

This should work:

 

<?php
$get_allteams = mysql_query("SELECT * FROM teams WHERE game = 'css' AND division = 'a'");
$team_matches = array();

// first setup a teams array
$i=0;
while ($team = mysql_fetch_assoc($get_allteams)) {
     $teams[$i++] = $team;
}

$i=0;
while (count($teams) >= 2) {
    // if there are less than 2 teams in the array, do not enter if.
    $index = rand(0,(count($teams) - 1));
    $get_home_team = $teams[$index];
    unset($teams[$index]); // remove the team from the list
    sort($teams);
    
$index = rand(0,(count($teams) - 1));
    $get_away_team = $teams[$index];
    unset($teams[$index]); 
sort($teams);

    $team_matches[$i]['away'] = $get_away_team;
    $team_matches[$i++]['home'] = $get_home_team;
}

// lets print out a test

print_r($team_matches);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.