I've looked through several other posts similar to this but can't find anything that suits what I'm looking for...
I've managed to hard code an 8 team tournament brackets table but I really want a way for the table to be drawn up using for/if statements based on the number of teams that are inputted.
Here's the code for hard coding the table using colspans and rowspans to create the blank spaces (and before you say use CSS instead of tables, I don't know CSS that well but am confident with tables):
<table border='1' cellspacing='1' cellpadding='1'>
<tr><td colspan='2' width='120'><b>Round 1</b></td></td><td colspan='2' width='120'><b>Round 2</b></td></td><td colspan='2' width='120'><b>Final</b></td><td width='120'><b>Winner</b></td></tr>
<tr><td colspan='2'> </td><td colspan='2' rowspan='2'></td><td colspan='2' rowspan='4'></td><td rowspan='8'></td></tr>
<tr><td>Group 1</td><td width='20'></td></tr>
<tr><td align='center'>Score 1</td><td></td><td>Round 1 Winner</td><td width='20'></td></tr>
<tr><td>Group 2</td><td></td><td rowspan='3' align='center'>Score 5</td><td></td></tr>
<tr><td colspan='2'></td><td></td><td>Round 2 Winner</td><td width='20'></td></tr>
<tr><td>Group 3</td><td></td><td></td><td rowspan='7' align='center'>Score 7</td><td></td></tr>
<tr><td align='center'>Score 2</td><td></td><td>Round 1 Winner<td></td><td></td></tr>
<tr><td>Group 4</td><td></td><td colspan='2' rowspan='3'></td><td></td></tr>
<tr><td colspan='2'></td><td></td><td>Champion</td></tr>
<tr><td>Group 5</td><td></td><td></td><td rowspan='8'></td></tr>
<tr><td align='center'>Score 3</td><td></td><td>Round 1 Winner</td><td></td><td></td></tr>
<tr><td>Group 6</td><td></td><td rowspan='3' align='center'>Score 6</td><td></td><td></td></tr>
<tr><td colspan='2'></td><td></td><td>Round 2 Winner</td><td></td></tr>
<tr><td>Group 7</td><td></td><td></td><td colspan='2' rowspan='3'></td></tr>
<tr><td align='center'>Score 4</td><td></td><td>Round 1 Winner</td><td></td></tr>
<tr><td>Group 8</td><td></td><td colspan='2'></td></tr>
</table>
This is what it produces:
And here's my attempt so far to create the above using for and if statements, I've got to the point where it lists the team names in the first column but I can't work out what I need to do from this point...
<?php
echo "<table border='1' cellspacing='1' cellpadding='1'>";
$num_teams = 8;
$nRounds = floor(log($num_teams,2));
$max_rows = $num_teams*2;
for ($row = 0; $row <= $max_rows; $row++)
{
echo "<tr>";
if ($row == 0)
{
for ($i = 1; $i <= $nRounds+1; $i++)
{
if($i < $nRounds)
{
echo "<td width='120'><b>Round ".$i."</b></td>";
echo "<td width='20'></td>";
}
elseif($i == $nRounds)
{
echo "<td width='120'><b>Final</b></td>";
echo "<td width='20'></td>";
}
else
{
echo "<td width='120'><b>Winner</b></td>";
}
}
}
elseif($row == 1)
{
$rowwhitespace = ($nRounds*2)+1;
echo "<td colspan='".$rowwhitespace."'> </td>";
}
else
{
$rowwhitespace = ($nRounds*2);
for ($i = 1; $i <= $num_teams; $i++)
{
if($i == $row/2)
{
echo "<td>Group ".$i."</td>";
echo "<td colspan='".$rowwhitespace."'></td>";
}
}
}
echo "</tr>";
}
echo "</table>";
?>
And here's what that looks like:
Ultimately the team names, scores and winners will be pulled from a DB so will need to incorporate that once I can get the structure laid out correctly.
I'd also like this to be scalable to 64 teams and to automatically draw the table based on that many teams without using manually hard code tables as this will takes ages to create tables for 16,32 and 64 teams. Coupled with considering double elimination for up to 64 teams which will make manually coding this a massive job!
Any pointers or assistance with this would be massively appreciated!