mercredi 5 février 2020

Drawing straws using PHP

CONTEXT

Lately, I've been trying to make php draw straws. If you don't know what drawing straws is, it's basically someone getting someone else's name and that someone else gets another someone else's name etc. To make it more clear, the randomly generated output of the code should look like this:

(example with 4 people)

Person A drew person B
Person B drew person D
Person C drew person A
Person D drew Person B

ATTEMPT 1

To achieve this, I've tried using the following code:

$name = $_POST["names"]; // <-- name-array
$participants = $_POST["participants"]; // <-- stores the amount of participants
$arrX = $name;

for($i = 1; $i <= $participants[0]; $i++) {
    echo $name[$i-1];
     echo " drew ";
     ${"randIndex$i"} = array_rand($arrX, 1);
     echo $arrX[${"randIndex$i"}];
     echo "<br><br>";
}

PROBLEM 1

While this does deliver an output, it is not satisfactory. A person can draw him/herself, which isn't allowed. I want the participants to draw a person other than themselves.

My best guess is that in order to fix this, I'd have to work with if-statements. I just don't know how I would do that the most fitting way.




Aucun commentaire:

Enregistrer un commentaire