PHP Script To Select A Person To Make The Tea

In any office there can be arguments about who will make the next round of tea. The following script will allow you to randomly pick a person who is going to make the tea. Rather than have a script that did this once and threw away the information I thought it would be a good idea to use cookies to save the form data for the next time you want to pick a person to make the tea. This is a good exercise if you are trying to understand how cookies work.

First, we will need to variables, the first is an array of people and the second is the number of people in the office.

$people = array();
$number = 10;

We can now build the form that will contain all of our names. Here we just cycle through a simple for loop and if an array item exists in the $people array for the value of $i then we use this in our form.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>;" method="get"><?php
for ($i = 0; $i < $number; $i++) {
  if (isset($people[$i])) {
    echo '<input type="text" data-cke-saved-name="people[]" name="people[]" value="' . $people[$i] . '" /><br />';
  } else {
    echo '<input name="people[]" type="text" value="" /><br />';
  }
}?>
<input name="submit" type="submit" value="Save &amp; Run" />
</form>

Next we need to grab the variables coming from the form and store them in a array, because we used a name with square brackets we can access the people GET variable as if it were an array.

if ( isset($_GET['people']) ) {
  $people = $_GET['people'];
}

We can now select who will be making the tea. Here I use a combination of array_filter() to remove all blank entries in the array and array_rand() to randomly select a key from the array. The array_rand() function returns NULL if it is given a blank array so we need to account for people first visiting the page.

if ($who = array_rand(array_filter($people))) {
    $teaMaker = $people[$who];
    echo '<p>' . $teaMaker . ' will be making the tea!</p>';
}

Enabling cookies with this script is quite easy and involves only a small amount of code addition. First we need to create a cookie if the people variable exists. Because this is an array and the setcookie() function requires a string we need to use the serialize() function to convert the $people array into a string value.


if (isset($_GET['people'])) {
    $people = $_GET['people'];
    setcookie("teaCookie", serialize($people));
}

Now that we have created our cookie we need to retrieve it again when the page reloads. All cookies are kept by reference in the $_COOKIE superglobal array. Once we are sure that the cookie exists we can user unserialize() to convert our serialized string into an array again.

if (isset($_COOKIE['teaCookie'])) {
    $people = unserialize($_COOKIE["teaCookie"]);
}

We now have the $people array in its original state.

Finally, rather than continuously setting and unsetting the cookie I have added a link that will allow you to pick another person from the list, without running the form. We first need to make sure that our cookie exists before allowing this link as it will do nothing.

<?php if (isset($_COOKIE['teaCookie'])) { ?>
  <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Pick Again!</a></p>
<?php } ?>

Of course this script isn't all that useful if you either don't drink tea or work alone, but it can easily be adapted to other functions.

Comments

This is a great scenario for the selection of who would make tea. Particularly suitable for a large group. Thank you for your version of the tea picker with source code.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.