Jump to content

Php Bracket Maker


smproph

Recommended Posts

Okay right now I have a form that pulls all the values I need and sends it to a separate page, except here is where I run into the problem. I logically have no idea who to write it. I have an array called $teamname that holds all the team names. What I need to do is make it dynamically create the table rows according to how many teams their are and then put the teams against each other like 1v8, 2v7. Here is my script that allows the bracket to work, I just need to convert it to dynamically work

<script>
function win(winner)
{
    var team = winner.value;
    var levels = winner.name.substring(3).split("_");
    var curlevel = parseInt(levels[0]);
    var curgame  = parseInt(levels[1]);

    var nextlevel = curlevel + 1;
    var nextgame  = Math.floor( (curgame+1) / 2 );
    
    var winnerButton = winner.form.elements["WIN"+nextlevel+"_"+nextgame];
    if ( winnerButton == null ) return;

    ++nextlevel;
    nextgame  = Math.floor( (nextgame+1) / 2 );
    var nextButton = winner.form.elements["WIN"+nextlevel+"_"+nextgame];
    var forward = ( nextButton != null && nextButton.value == winnerButton.value );

    winnerButton.value = team;
    if ( forward ) winnerButton.click( );
}
</script>

<?php
$schedule=$_POST['schedule'];

if($schedule=="season")
	{
		$teams=$_POST['teams'];
		$schedule=$_POST['schedule'];
		$teamname = unserialize(urldecode($_POST["teamname"])); 
		$games=$_POST['games'];
		$times=$_POST['times'];

	}
if($schedule=="playoffs")
	{
		$teams=$_POST['teams'];
		$schedule=$_POST['schedule'];
		$teamname = unserialize(urldecode($_POST["teamname"]));
		$games=$_POST['games'];
		$times=$_POST['times'];
		$result = count($teamname);

?>
<form>
<table border=0 cellpadding=3>
<tr>
    <td><input type=button class="team" name="WIN0_1" onclick="win(this)" value="Amherst"></td>
</tr>
<tr>
    <td></td>
    <td><input type=button class="team" name="WIN1_1" onclick="win(this)" value=""></td>

</tr>
<tr>
    <td><input type=button class="team" name="WIN0_2" onclick="win(this)" value="Bowling Green"></td>
</tr>
<tr>
    <td> </td>
    <td> </td>
    <td><input type=button class="team" name="WIN2_1" onclick="win(this)" value=""></td>
</tr>
<tr>
    <td><input type=button class="team" name="WIN0_3" onclick="win(this)" value="Connecticut"></td>
</tr>
<tr>
    <td></td>
    <td><input type=button class="team" name="WIN1_2" onclick="win(this)" value=""></td>
</tr>
<tr>
    <td><input type=button class="team" name="WIN0_4" onclick="win(this)" value="Duke"></td>
</tr>
<tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td><input type=button class="team" name="WIN3_1" onclick="win(this)" value=""></td>
</tr>
<tr>
    <td><input type=button class="team" name="WIN0_5" onclick="win(this)" value="Elmira"></td>
</tr>
<tr>
    <td></td>
    <td><input type=button class="team" name="WIN1_3" onclick="win(this)" value=""></td>

</tr>
<tr>
    <td><input type=button class="team" name="WIN0_6" onclick="win(this)" value="Florida"></td>
</tr>
<tr>
    <td> </td>
    <td> </td>
    <td><input type=button class="team" name="WIN2_2" onclick="win(this)" value=""></td>
</tr>
<tr>
    <td><input type=button class="team" name="WIN0_7" onclick="win(this)" value="Georgetown"></td>
</tr>
<tr>
    <td></td>
    <td><input type=button class="team" name="WIN1_4" onclick="win(this)" value=""></td>
</tr>
<tr>
    <td><input type=button class="team" name="WIN0_8" onclick="win(this)" value="Hawaii"></td>
</tr>

</table>
</form>
<?
	}
	?>

 

Link to comment
Share on other sites

Well right now, since I have only entered 4 teams in, it has Team 1 Team 2 Team 3 Team 4 as my array

 

Array ( [0] => team1 [1] => team2 [2] => team3 [3] => team4 )

 

Not sure if I understood what you meant when you said

Are you wanting to generate fixtures for the teams, or just display fixtures according to a particular order?

 

I have it where you type in the teams in order by their seeds. So team1 is Seed #1, Team 2 is Seed #2 and so on... I am trying to get it to where it sees how many teams you put in, maybe with a count function on the array and then put them against each other, Seed 1 vs Seed 8.

 

Does that make any sense?

Link to comment
Share on other sites

I wrote this:

$i=1;
$j=1;
$k=1;
$run=1;
$run2=0;
while($run<=$numofteams)
{
$name="WIN0_'".$i."'";
echo'<tr>
    <td><input type=button class="team" name="'.$name.'" onclick="win(this)" value="'.$teamname[$run].'"></td>
</tr>';
$run++;

	if($run==2)
	{
		$name2="WIN1_'".$j."'";
		echo '<tr>
   					 <td></td>
   						 <td><input type=button class="team" name="'.$name2.'" onclick="win(this)" value=""></td>
				</tr>';

					if($run2==2)
							{
						$name3="WIN'".$k."'_1";
								echo '<tr>
   										 <td></td>
   											 <td><input type=button class="team" name="'.$name3.'" onclick="win(this)" value=""></td>
										</tr>';
										$run2=0;
										$k++;
							}
				$run=0;
				$run2++;
				$j++;
	}

$i++;
}

 

It almost gets it but just repeats 10,000 times and only fills in team 1 and team 2

 

Link to comment
Share on other sites

I added it and wrote it like this

	$teams=$_POST['teams'];
		$schedule=$_POST['schedule'];
		$teamname = unserialize(urldecode($_POST["teamname"]));
		$games=$_POST['games'];
		$times=$_POST['times'];
		$numofteams=(count($teamname)/2);

 

Simon yours works perfect other than the fact I am trying to implement it to those <tr><td> because it makes it where you can click on the winner and it automatically advances them to the next bracket. It connects to my javascript I wrote thats in my first post.

 

Link to comment
Share on other sites

Here is something that works as a standalone example. You will need to customise it to work with your script, but I suspect you will find that relatively straight forward. The most difficult thing here was getting the mathematics right in order to place the rows in the right place.

 

<?php
$array = array('t1', 't2', 't3', 't4', 't5', 't6', 't7', 't8');
$j = count($array);
for ($a=0; pow(2, $a) <= $j; $a++) //determine the highest power of 2 that will go into the array count
{
$maxpower = $a;
}
for ($i=1; $i < $j*2; $i++) //needs almost twice as many rows as teams
{
if($i % 2 != 0) //odd number rows for teams
{
	$line[$i] = '<td><input type=button class="team" value="' . $array[($i-1)/2] . '"></td>';
}
else
{
	for($b=0; $b<=$maxpower; $b++)
	{
		if($i % pow(2, $b) == 0) //even rows for future rounds. every 2^1 rows for first winner, 2^2 for second winner, 2^3 for third and so on.
		{
			if($i % pow(2, $b+1) != 0) //does not divide by the next power of 2, so this must be the last available cell
			{
				$line[$i] .= '<td><input type=button class="team" value=""></td>';
			}
			else //the input will be added in a future round
			{
				$line[$i] .= '<td> </td>';
			}
		}
	}
}
}
?>
<html>
<body>
	<table>
<?php
foreach($line as $row)
{
?>
		<tr>
			<?php echo $row; ?>

		</tr>
<?php
}
?>
	</table>
</body>
</html>

Link to comment
Share on other sites

Populates great other than

name="WIN1_1" onclick="win(this)"

is missing in the input tag. But I think i should be able to write something where it changes the "WIN1_1" to "WIN1_2" on the next one and so on

 

each row has a value $i - perhaps that could be useful. If not, you should be able to create a new incrementing value as you work through the loops. I didn't go too deeply into that, as I'm not familiar with your code and didn't want to interfere with what you've done already.

Link to comment
Share on other sites

I've modified to this

 

$array = $teamname;
$k=1;
$x=1;

 

$line[$i] = '<td><input type=button class="team" onclick="win(this)" value="' . $array[($i-1)/2] . '">name="WIN0_'.($k).'"</td>';
$k++;

 

and this

$line[$i] .= '<td><input type=button class="team" onclick="win(this)" value="">name="WIN1_'.($y+1).'"</td>';
				$y=$x;

 

Problem is it starts to repeat itself into future rounds. First round works perfect, second

ss.jpg

 

 

 

Link to comment
Share on other sites

Try this:

 

<?php
$array = array('t1', 't2', 't3', 't4', 't5', 't6', 't7', 't8');
$j = count($array);
for ($a=0; pow(2, $a) <= $j; $a++) //determine the highest power of 2 that will go into the array count
{
$y[$a] = 1;
$maxpower = $a;
}
for ($i=1; $i < $j*2; $i++)
{
if($i % 2 != 0) //odd number rows for teams
{
	$line[$i] = '<td><input type=button class="team" value="' . $array[($i-1)/2] . '"></td>';
}
else
{
	for($b=0; $b<=$maxpower; $b++)
	{
		if($i % pow(2, $b) == 0) //even rows for future rounds. every 2^1 rows for first winner, 2^2 for second winner, 2^3 for third and so on.
		{
			if($i % pow(2, $b+1) != 0) //does not divide by the next power of 2, so this must be the last available cell
			{
				$line[$i] .= '<td><input type=button class="team" value="">name="WIN'.$b.'_'.($y[$b]++).'</td>';
			}
			else //the input will be added in a future round
			{
				$line[$i] .= '<td> </td>';
			}
		}
	}
}
}
?>
<html>
<body>
	<table>
<?php
foreach($line as $row)
{
?>
		<tr>
			<?php echo $row; ?>

		</tr>
<?php
}
?>
	</table>
</body>
</html>

 

particular changes:

 

$y[$a] = 1;

name="WIN'.$b.'_'.($y[$b]++)

Link to comment
Share on other sites

Simon once again that worked perfect. Last question, is there a way to have a save option? So the user doesn't lose it.

 

Yes, there are a few options, but anything you do would take time to implement, as you would need to have a corresponding load option. The biggest obstacle would be getting the data from where it is stored, into the relevant rows.

You could:

Store the data locally (suitable on a temporary basis) by using cookies

Save the data to a database, then give the user a permalink or a password to access it

Output the data to some kind of file, like CSV, Json or XML and then get the user to download it.

 

I think the database would be your best option, but you would need to structure the tables properly and then develop the queries to write to and read from the database.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.