create bracket data using php

47 Views Asked by At

I need to create bracket for that I need all values to be formed in array so that it can be placed in the brackets so the main problem i am getting is i am not pretty sure how do I organize data because all matches are added in the db we just need to print data in the form of array

| ID | tournamentID  | groupID | player_1_ID | player_2_ID | winnerID | Round |
| -- | ------------- | ------- | ----------- | ----------- | -------- | -------- |
| 10 |      74       |    1    |     35      |     36      |    0     | 1 |

Group ID is used for the matches i.e Group ID 1 belongs to 1st match and 2nd match in the bracket

Output Required

Array
(
    [0] => Array
        (
            [round_title] => Round 1
            [bracketData] => Array
                (
                    [0] => Array
                        (
                            [player_1_ID] => 35
                            [score_1] => 0
                            [player_2_ID] => 36
                            [score_2] => 0
                        )
                    [1] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                    [2] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                    [3] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                )

        )

    [1] => Array
        (
            [round_title] => Round 2
            [round] => 2
            [bracketData] => Array
                (
                    [0] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                    [1] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                )
        )

    [2] => Array
        (
            [round_title] => Round 3
            [round] => 3
            [bracketData] => Array
                (
                    [0] => Array
                        (
                            [player_1_ID] => 
                            [score_1] => 0
                            [player_2_ID] => 
                            [score_2] => 0
                        )
                )
            
        )

I am confused as of I have total number of players I have total number of rounds I have total number of matches in 1st round 2nd round as well as 3rd and so on and so forth

Below is what I tried to did is

$rounds = $this->get_rounds($tournamentID);
$totalPlayers = $this->get_data('tournament_players', array('tournamentID' => $tournamentID));
$playersCount = count($totalPlayers); 
$totalRounds =  strlen(decbin($playersCount - 1));
$key = 0;
for($i = 1; $i <= $totalRounds; $i++) {
$query = "SELECT * FROM tournament_matches WHERE tournamentID = '" . $tournamentID ."' AND round = '" . $round . "' AND seed = 1 ORDER BY groupID ASC";
         $matchesData = $this->db->query($query)->result();
         $roundTitle  = 'Round ' . $round;
         $bracket[$key] = array(
             'round_title' => $roundTitle,
             'round' => $round,
         );

         if(count($matchesData) > 0) {
             foreach($matchesData as $match):
                 $bracket[$key]['bracketData'][$key_count_match] = array(
                     'player_1_ID' => $match->player_1_ID,
                     'score_1'     => $player_1_score,
                     'player_2_ID' => $match->player_2_ID,
                     'score_2'     => $player_2_score
                 );
                 $key_count_match++;
             endforeach;
         } 

         
   $key++;
}

I know the code I developed is totally a disaster I am not doing good thing but is there a proper way to do so or is there any way I can do this

Fiddle link is as follows

https://phpize.online/s/q7
0

There are 0 best solutions below