I have a question about randomly assigning users in my database using PHP. I want to create a website where people enter the number of users in the first step and the names of these users in the second step. Then the names should be mixed up randomly and each name has been assigned a different name (which must not happen: get yourself or a person assigned twice). This website is a small project for my family. We could always use them at Christmas when we make some kind of drawing. Everyone draws a name from the participants and then gets a present for the person drawn (I don't know how it is called in English. In Germany we say “wichteln”: D). Unfortunately I don't know how to do this in PHP.
So far, in the first step I have asked how many participants there are. It then checks whether the number entered is a number between 2 and 49. In the second step the names of the participants are given. When all names are entered, a button (name = teilnehmerdetails) is clicked. Then the following is carried out:
if(isset($_POST['teilnehmerdetails'])) {
$datetime = date("Y-m-d H:i:s");
$date = date("Ymd");
$time = date("His");
$generatorid = $date . $time;
$name = $_POST['name'];
foreach( $name as $key => $n ) {
$teilnehmerid = $key + 1;
$query = "INSERT INTO member (generator_id, date, userid, name) VALUES ('$generatorid', '$datetime', '$teilnehmerid', '$n')";
$query_conn = mysqli_query($connection, $query);
if(!$query_conn) {
die("Query failed" . mysqli_error($connection));
}
echo $teilnehmerid.". Teilnehmer: ".$n;
echo "<br>";
}
}
The participants are now stored in the database. Using the variable $generatorid, I can see which participants belong to the request.
How can the entered names be mixed up and assigned a new name to each one?
E.g. entered the following names: Person 1, Person 2, Person 3, Person 4
A random result is then: Person 1 has Person 3, Person 2 has Person 1, Person 3 has Person 4, Person 4 has Person 2