smproph Posted January 24, 2011 Share Posted January 24, 2011 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> <? } ?> Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 24, 2011 Share Posted January 24, 2011 Are you wanting to generate fixtures for the teams, or just display fixtures according to a particular order? Also, what is the exact data in your $teamname array? i.e. what happens if you print_r() it? Quote Link to comment Share on other sites More sharing options...
smproph Posted January 25, 2011 Author Share Posted January 25, 2011 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? Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 25, 2011 Share Posted January 25, 2011 Will something like this do? <?php $array = array('t1', 't2', 't3', 't4', 't5', 't6', 't7', 't8'); $j = count($array); for ($i=0; $i < $j/2; $i++) { echo $array[$i] . ' V ' . $array[$j - $i - 1] . '<br/>'; } ?> Quote Link to comment Share on other sites More sharing options...
smproph Posted January 25, 2011 Author Share Posted January 25, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 25, 2011 Share Posted January 25, 2011 where do you define $numofteams? Quote Link to comment Share on other sites More sharing options...
smproph Posted January 25, 2011 Author Share Posted January 25, 2011 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. Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 25, 2011 Share Posted January 25, 2011 Just so I understand, are you wanting to produce something like this? A -A or B B --A or B or C or D C -C or D D Quote Link to comment Share on other sites More sharing options...
smproph Posted January 25, 2011 Author Share Posted January 25, 2011 Exactly, It's just like the March Madness bracket if you have ever seen one of those. You have Quarterfinals, then Semi Finals then Finals Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 25, 2011 Share Posted January 25, 2011 I think I can do something, but it probably won't be tonight. In case it helps, I've worked out that the teams appear on every other row in round 1 on every fourth row in round 2, every eighth row in round 3, etc. Quote Link to comment Share on other sites More sharing options...
smproph Posted January 25, 2011 Author Share Posted January 25, 2011 Take your time, and thanks. I thought that code I wrote above would work, but for some reason it doesn't like me lol Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 26, 2011 Share Posted January 26, 2011 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> Quote Link to comment Share on other sites More sharing options...
smproph Posted January 26, 2011 Author Share Posted January 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 26, 2011 Share Posted January 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
smproph Posted January 26, 2011 Author Share Posted January 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
Mr Hyde Posted January 26, 2011 Share Posted January 26, 2011 I don't like to write code when I can just use an open source project: http://www.phpclasses.org/package/4718-PHP-Manage-knockout-tournament-games.html phpclasses.org is an awesome site... Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 26, 2011 Share Posted January 26, 2011 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]++) Quote Link to comment Share on other sites More sharing options...
smproph Posted January 26, 2011 Author Share Posted January 26, 2011 Simon once again that worked perfect. Last question, is there a way to have a save option? So the user doesn't lose it. Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted January 26, 2011 Share Posted January 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
smproph Posted January 26, 2011 Author Share Posted January 26, 2011 Awesome, Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.