Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

handy_man's avatar

Tournament bracket creation? sorting other than ASC or DESC?

I've been working on a personal project for me to take a base website with users and create and manage tournament brackets within the website.

I've been having some trouble trying to figure out where to go from where I am currently, for example I have players that can signup to tournaments via a signup button and I can return via a pivot table the user objects of all users signed upto the tournament.

How do I then take that data and figure out how many matches need to take place? assign them and even seed the users? I'm just completely confused how I can for example go through a list of user objects and throw them into a match 2 at a time then change the match ID and continue? what about seeding? How can I sort this list of users based on a points column or something similar?

0 likes
1 reply
beznez's avatar

I'm guessing you are doing a standard tournament bracket style. So each level of the tournament corresponds to an amount of users that is a power of 2. 2^1 = 2 users, who would then play 1 game. 2^2 = 4 users, who would then play 2 games and then the winners play a game: 2+1=3. See the pattern? If you have a tournament with n levels, then it will have 2^n teams and the games you would need to play are (2^n)-1. If this is still confusing, draw it out, check my numbers, and then do the third level and the pattern will become more apparent. This may be helpful:

Levels  Users   Matches
1       2       1
2       4       3
3       8       7
4       16      15

As far as assigning and seeding users, I think your best option is to make a matches table that would keep track of all of your matches. Of course you would need to have a tournament id, user id's and anything else that is relevant for you. When you seed your users, then you can just insert them into your database. For example:

$match = new Match;
$match->tournament_id = $tournament_id;
$match->player_one_id = $player_one->id;
$match->player_two_id = $player_two->id;
$match->round = $round_number;
$match->winner = null; // Set that when the player wins
$match->save();

You can sort on a column like this:

Users::all()->orderBy('points', 'desc');
1 like

Please or to participate in this conversation.