tomd79 Posted June 27, 2008 Share Posted June 27, 2008 Hi, New to PHP, I have been reading but am in serious need of some direction/help....... I have a simple HTML check box form with 20 options for the user to select. All I want is to have the selected options divided randomly into 2 groups and displayed back to the user.. PHP seems like the right thing to do this in, but I'm completely stuck.. Can anyone offer me some help guidance? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/ Share on other sites More sharing options...
hitman6003 Posted June 27, 2008 Share Posted June 27, 2008 put them into an array, then use the array_shuffle function http://www.php.net/array_shuffle $data = array_map(create_function('$a', 'return "option_" . $a;'), range(1, 20)); foreach ($data as $option) { echo '<input type="checkbox" value="' . $option . '"> ' . $option . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575885 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 Hi, thanks for the input.. Before I start reading on array shuffle, can I ask 2 things.. Will this allow me to assign to 2 separate groups? and will this still work if the user only picks say 15 of the options available? basically I want the user to choose who will be playing the football mini league that week and split the users into 2 teams.. Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575894 Share on other sites More sharing options...
hitman6003 Posted June 27, 2008 Share Posted June 27, 2008 Sorry, got distracted and gave you an incomplete example... <?php $data = array_map(create_function('$a', 'return "option_" . $a;'), range(1, 20)); $data = shuffle($data); echo "Group One:<br />"; for ($i = 0; $i < 10; $i++_ echo '<input type="checkbox" value="' . $data[$i] . '"> ' . $data[$i] . '<br />'; } echo "Group Two:<br />"; for ($i = 10; $i < 20; $i++_ echo '<input type="checkbox" value="' . $data[$i] . '"> ' . $data[$i] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575899 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 Thanks for the further input, I think this is a case or running before walking! :/ I have taken a step back and trying to just get an array into PHP.. My form to set up the array in the html <form name="players[] "action="sort.php" method="POST"> <input type="checkbox" name="players[]" value="1" /> 1<br /> <input type="checkbox" name="players[]" value="2" /> 2<br /> <input type="checkbox" name="players[]" value="3" /> 3<br /> <input type="checkbox" name="players[]" value="4" /> 4<br /> <input type="checkbox" name="players[]" value="5" /> 5<br /> <input type="submit" value="Pick the teams!"> </form><br /> The code in my sort PHP print the content of the array (to see if Im doing the form bit right) <?php $players = $_POST["players"]; echo $players; ?> This simply prints "Array" in the subsequent HTML!? Anyone tell me where Im goiung wrong? thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575929 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 use print_r(); Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575959 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 Xyn - Thanks to you.. have used print_r($players); and the subsequent html shows the form working.. Small step but learning Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575978 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 Can I just ask what you're trying to archive? and what your problem is? I'll try and code you something to learn from. Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-575986 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 hi again, at work we play 5 aside football each week. There are 20 people who play, but not all play every week.. So I wanted a form which lists all the possible players, we can tick the check boxes next to those who CAN play that week, then submit. This would then return the selected played assigned Randomly to either Team A or Team B.. I hope that makes sense.. And thanks so much for taking the time to help, I've only got as far as printing the array from the form and I've already picked up loads! Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576009 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 Ok, do you store your players in a database? and would you onyl have team A and team B? Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576021 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 Sorry, I got lost. so you want to be able to check say 10 players? and then 5 are randomly places in team A, and the other 5 are randomly placed in team B? Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576030 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 Hi, Sorry for taking a while to get back to you! 1. There will only ever be 2 team. 2. The number of players playing each week varies (and they can be uneven numbers) 3. The players are stored in a list of check boxes in a html form the form holding the names: <form action="sort.php" method="POST"> <input type="checkbox" name="players[]" value="name0" /> name 0<br /> <input type="checkbox" name="players[]" value="name1" /> name 1<br /> <input type="checkbox" name="players[]" value="name2" /> name 2<br /> <input type="checkbox" name="players[]" value="name3" /> name 3<br /> <input type="checkbox" name="players[]" value="name4" /> name 4<br /> <input type="checkbox" name="players[]" value="name5" /> name 5<br /> <input type="checkbox" name="players[]" value="name6" /> name 6<br /> <input type="checkbox" name="players[]" value="name7" /> name 7<br /> <input type="checkbox" name="players[]" value="name8" /> name 8<br /> <input type="checkbox" name="players[]" value="name9" /> name 9<br /> <input type="checkbox" name="players[]" value="name10" /> name 10<br /> <input type="checkbox" name="players[]" value="name11" /> name 11<br /> <input type="checkbox" name="players[]" value="name12" /> name 12<br /> <input type="checkbox" name="players[]" value="Guest 1" /> Guest 1: ()<br /> <input type="checkbox" name="players[]" value="Guest 2" /> Guest 2: ()<br /> <br /> <input type="submit" value="Pick the teams!"> </form><br /> So its something like: Select players from list: Pass selected players to PHP array: Randomly assign one player at a time to group A or group B: When the array is empty, display the groups with their assignments.. Thanks again so much for your help! Tom Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576258 Share on other sites More sharing options...
discomatt Posted June 27, 2008 Share Posted June 27, 2008 This is quite easy actually. You've made it to the point where $_POST['players'] has an array of the selected players.... the rest is easy! <?php # On your form action page # First we'll make a quick function to display # our arrays, if you get lost here, don't worry, # it's not necessary! function show( $title, $array ) { echo "<h3>$title</h3>\n<pre>\n"; print_r( $array ); echo "</pre>\n<br /><br />\n"; } # Okay, now the actual fun! First lets show the # original array, using the above function show( "Untouched Results", $_POST['players'] ); # Now we can randomize the order they're in shuffle( $_POST['players'] ); # Lets show this new, shuffled list show( "Shuffled Results", $_POST['players'] ); # Okay, now the work begins... there's no quick way to split # an array in half, but there's a nice function called # array_chunk(), which splits an array into chunks - this might # work, but first we need to know how big the chunks will be... # My guess would be 1/2 the size (rouded up) of the total # players, so lets get that! # Find out how many entries were selected using count() $player_count = count( $_POST['players'] ); # Now we want to find the size of the largest team, and make # that... so we divide the player count by 2, and round up # in case of an odd number of players $team_size = ceil( $player_count / 2 ); # Now we'll create 2 arrays using the list function, each # containting a different group of players list( $team1, $team2 ) = array_chunk( $_POST['players'], $team_size ); # Show off team1 and team2 show( "Team 1", $team1 ); show( "Team 2", $team2 ); # Now I'll show you a prettier way to show off the results echo "<h2>Team 1</h2>"; echo "<b>Player count:</b> " . count( $team1 ) . "<br />"; echo "<b>Player list:</b>"; echo "<ul>"; # foreach will loop through our array foreach ( $team1 as $player ) { echo "<li>$player</li>"; } echo "</ul>"; echo "<h2>Team 2</h2>"; echo "<b>Player count:</b> " . count( $team2 ) . "<br />"; echo "<b>Player list:</b>"; echo "<ul>"; foreach ( $team2 as $player ) { echo "<li>$player</li>"; } echo "</ul>"; ?> If you have any questions the comments don't cover, check the PHP manual for the specific function. If that doesn't help explain, ask here Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576288 Share on other sites More sharing options...
discomatt Posted June 27, 2008 Share Posted June 27, 2008 Here's a shorter version, that does the same thing, but doesn't use list() to make 2 arrays. It instead makes a single multi-dimensional array, and uses nested foreach loops to get all the information we need. <?php shuffle( $_POST['players'] ); $team_size = ceil( count($_POST['players']) / 2 ); $teams = array_chunk( $_POST['players'], $team_size ); foreach( $teams as $num => $team ) { echo "<h2>Team ". ($num+1) ."</h2>"; echo "<b>Size:</b> ". count($team) ."<br />"; echo "<b>Player List:</b>"; echo "<ul>"; foreach( $team as $player ) echo "<li>$player</li>"; echo "</ul>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576292 Share on other sites More sharing options...
tomd79 Posted June 27, 2008 Author Share Posted June 27, 2008 Discomatt, sent you an email.. I'm going to take a while to understand what you have put together, but thank you very much for taking the time to help a stranger out!!! Quote Link to comment https://forums.phpfreaks.com/topic/112181-solved-randomly-splitting-form-data-into-2-groups/#findComment-576313 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.