Jump to content

[SOLVED] Randomly splitting form data into 2 groups


Recommended Posts



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?



Link to comment
Share on other sites

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..

Link to comment
Share on other sites

Sorry, got distracted and gave you an incomplete example...



$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 />';

Link to comment
Share on other sites

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)

$players = $_POST["players"];
echo $players;


This simply prints "Array" in the subsequent HTML!?


Anyone tell me where Im goiung wrong?


thanks so much




Link to comment
Share on other sites

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!

Link to comment
Share on other sites



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!


Link to comment
Share on other sites

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!



# 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 :)

Link to comment
Share on other sites

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.



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>";



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.

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.