"Player", ...)
* round_num = 0-based contiguous index
* player_num = 0-based contiguous index (the number of players is cut in half each round)
*/
// You are responsible for building up your bracket for each round using your own logic to determine who advances to the next round
// The winner of match [0][0] and [0][1] will be stored in [1][0], the winner of match [0][2] and [0][3] will be stored in [1][1], and so on
$bracket = array();
$rounds = 6;
for ($i = 0; $i < $rounds; $i++)
for ($j = 0; $j < pow(2, ($rounds-$i-1)); $j++)
$bracket[$i][$j] = array("name" => "Player " . ($j*pow(2, $i)));
// Now let's see what happens if we have an unfilled bracket
unset($bracket[0][15]);
unset($bracket[0][31]);
// This logic to ensure a filled bracket should occur after the initial logic to build your bracket
// This will only ensure a filled bracket for the first round of competition (which is most common), but the logic could easily be expanded to fill the bracket at any round
$num = count($bracket[0]);
// Not a full bracket (not a power of 2), add byes
if (($num & ($num-1)) != 0)
{
// This is the size the bracket should be
$num = pow(2, ceil(log10($num)/log10(2)));
for ($i = 0; $i < $num; $i++)
if (!isset($bracket[0][$i]))
$bracket[0][$i] = array("name" => "Bye");
}
// And now we actually draw the bracket
// The trick here is to create a grid using a table
// Only the cells containing a player will be "turned on" (border set)
// Cells located vertically between players in a given round will have their right border set to create the bracket effect
print "\n";
$height = count($bracket[0])*2-1;
$width = count($bracket);
for ($i = 0; $i < $height; $i++)
{
print "";
for ($j = 0; $j < $width; $j++)
{
// These variables are used to determine which cells to draw data in
$start = pow(2, $j)-1;
$offset = pow(2, ($j+1));
// This variable is used to map the grid position back to the 0-based contiguous array index for a given round
// I feel like this equation could be simplified further, but I just couldn't find it
$num = (($i+1) / pow(2, ($j+1))) - 0.5;
$hasdata = ($i == $start || ($i - $start) % $offset == 0);
$hasborder = ($j < ($width-1) && floor($num) % 2 == 0);
$classes = array();
if ($hasdata)
$classes[] = "withborder";
if ($hasborder)
$classes[] = "borderright";
$classes = implode(" ", $classes);
print "" . ($hasdata ? $bracket[$j][$num]["name"] . (isset($bracket[$j][$num]["result"]) ? " - " . $bracket[$j][$num]["result"] : "") : "") . "";
}
print " | \n";
}
print "\n";
?>