Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

single elimination php code

<?php

function is_player($round, $row, $team) {
    return $row == pow(2, $round-1) + 1 + pow(2, $round)*($team - 1);
}

$num_teams = 16;
$total_rounds = floor(log($num_teams, 2)) + 1;
$max_rows = $num_teams*2;
$team_array = array();
$unpaired_array = array();
$score_array = array();

for ($round = 1; $round <= $total_rounds; $round++) {
    $team_array[$round] = 1;
    $unpaired_array[$round] = False;
    $score_array[$round] = False;
}


echo "<table border="1" cellspacing="1" cellpadding="1">
";
echo "	<tr>
";

for ($round = 1; $round <= $total_rounds; $round++) {

    echo "		<td colspan="2"><strong>Round $round</strong></td>
";

}

echo "	</tr>
";

for ($row = 1; $row <= $max_rows; $row++) {

    echo "	<tr>
";

    for ($round = 1; $round <= $total_rounds; $round++) {
        $score_size = pow(2, $round)-1;
        if (is_player($round, $row, $team_array[$round])) {
            $unpaired_array[$round] = !$unpaired_array[$round];
            echo "		<td>Team</td>
";
            echo "		<td width="20"> </td>
";
            $team_array[$round]++;
            $score_array[$round] = False;
        } else {
            if ($unpaired_array[$round] && $round != $total_rounds) {
                if (!$score_array[$round]) {
                    echo "		<td rowspan="$score_size">Score</td>
";
                    echo "		<td rowspan="$score_size" width="20">$round</td>
";
                    $score_array[$round] = !$score_array[$round];
                }
            } else {
                echo "		<td colspan="2"> </td>
";
            }
        }

    }

    echo "	</tr>
";

}

echo "</table>
";

?>
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #single #elimination #php #code
ADD COMMENT
Topic
Name
5+2 =