Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Creating a league fixture list - php

Options
  • 03-02-2007 1:36pm
    #1
    Registered Users Posts: 673 ✭✭✭


    Hi,

    Im trying to set up a league fixture list. I have set up the script to put league players into a mysql table but dont know how to go about doing the fixtures. Somone has suggested using a dandomize function and put that into an array but i dont have too much php experience and im still not sure how to do that.

    Can anyone point me in the right direction?

    Thanks


Comments

  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    I did one in java not so long ago.

    Setting the fixtures is quite easy, in java I set up a temporary Hashtable. Each Hashtable has num_of_games rows, each row contains an Object of type Fixture, and Fixture has home team/away team.

    The logic then goes (Assume there's 20 rounds, this is determined by the number of players, rounds = num_players - 1 )

    Use a loop to populate the table once, randomizing the player ids from the database, making sure that on each round, neither of the teams has already been used. You can do this by iterating through the fixtures already in the temporary Hashtable, and checking the values for hometeam and away team. If hometeam OR awayteam NOT equals CURRENT_ID, addFixturetoHashtable, get next ID from randomizer.

    Added to this, you need an outer loop that will generate 20 rounds, but you need to check the fixtures to be inserted, against fixtures already in the table, obviously you need to ensure that each team plays every other team home and away.

    Would it help if I sent you the code for this?


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Thanks, dont know if their would be much point in sending the code as i know nothing about java. barely know php ;)

    Thanks for the info though, jives me another way of looking at it.

    I have gotten this far with the help of a friend who knows c++, im putting all the values into two array and then randomly picking a number from array one, then randomly picking a number from array 2 and changing their array value to -1 after so i dont use it again:
    $my_array_1 = array(4, 7, 9);
    $my_array_2 = array(4, 7, 9);
    
    while ($my_array != array(-1, -1, -1) OR $my_array_2 != array(-1, -1, -1)) {
    
    $fixture_1 = $my_array_1[rand(0, 2)];
    $fixture_2 = $my_array_2[rand(0, 2)];
    
    echo "fixture 1 = ".$fixture_1."<br>";
    echo "fixture 2 = ".$fixture_2."<br><br>";
    
    echo "my array 1-0 = ".$my_array[0]."<br>";
    echo "my array 1-1 = ".$my_array[1]."<br>";
    echo "my array 1-2 = ".$my_array[2]."<br><br>";
    
    echo "my array 2-0 = ".$my_array_2[0]."<br>";
    echo "my array 2-1 = ".$my_array_2[1]."<br>";
    echo "my array 2-2 = ".$my_array_2[2]."<br><br><br>";
    echo "-----------------------------------------<br><br>";
    
    if ($fixture_1 == 1) {
    $my_array[0] = -1;
    }
    if ($fixture_1 == 10) {
    $my_array[1] = -1;
    }
    if ($fixture_1 == 15) {
    $my_array[2] = -1;
    }
    
    if ($fixture_2 == 1) {
    $my_array_2[0] = -1;
    }
    if ($fixture_2 == 10) {
    $my_array_2[1] = -1;
    }
    if ($fixture_2 == 15) {
    $my_array_2[2] = -1;
    }
    

    but instead of the value i have in the array i want to populate this from values i have stored in a mysql database.

    Ive tried using this select query
    $sql = "SELECT user_id
    FROM my_table
    WHERE field1 = 62
    ";
    $results = mysql_query($sql)
    or die(mysql_error());
    
    while($rows = mysql_fetch_array($results)) {
    }
    

    but dont know how to get those values into my array.

    Im well confused with it all now so i might be missing something basic here. Anyone know what i should be doing?

    I know the scripta bout wont do exactly what im after i.e. make sure a team doesnt play another team twice but ill cross that bridge when i come to it.

    Thanks


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Not sure in PHP but the steps you need to take Id say are.

    Get number of entries from resultselt in database
    SELECT COUNT(user_id) AS count FROM my_table
    
    Divide count by two (for the length of each array). If there is a remainder, handle that using modulus.
    Then create two resultsets
    SELECT user_id FROM my_table LIMIT 0, $arraysize;
    SELECT user_id FROM my_table LIMIT $arraysize+1, $arraysize*2;
    
    And populate two arrays using these results. In java that would be (assuming Resultset = rs has already been set)
    int[] i = new int[$arraysize1];
    int counter = 0;
    while(rs.next()){
        i[counter] = rs.getInt("user_id");
        counter++;
    }
    
    Do this for both arrays, and you should have two populated arrays.

    Sorry, I dont know PHP, but I imagine the construction is similar. Let me know if this helps.

    Its probably an arse-about way of doing it, but I tend to think pseudo first, and then streamline and optimise later.

    HTH,
    Stephen


Advertisement