admin管理员组文章数量:1122846
I have an event with 10 spaces available for booking. When a user registers for the event, I need to assign them a random draw number from 1 through 10. I will include this number in the confirmation email they receive after registration.
I know I can generate the number like this:
$num = random_int(1, 10);
But I can't figure out how to ensure that the number assignment is unique. I can't give someone a number that has already been assigned to another registrant. People will not register at the same time, so I somehow need the function to check against all the previously assigned numbers before assigning a random number from the remaining numbers.
If it matters, event registrants will only be logged-in users, so I could assign something to their user meta if needed.
How would you go about this? Thank you!
I have an event with 10 spaces available for booking. When a user registers for the event, I need to assign them a random draw number from 1 through 10. I will include this number in the confirmation email they receive after registration.
I know I can generate the number like this:
$num = random_int(1, 10);
But I can't figure out how to ensure that the number assignment is unique. I can't give someone a number that has already been assigned to another registrant. People will not register at the same time, so I somehow need the function to check against all the previously assigned numbers before assigning a random number from the remaining numbers.
If it matters, event registrants will only be logged-in users, so I could assign something to their user meta if needed.
How would you go about this? Thank you!
Share Improve this question edited Jul 26, 2024 at 19:50 LBF asked Jul 26, 2024 at 0:58 LBFLBF 5293 gold badges11 silver badges28 bronze badges 2- it looks like you find the answer yourself : "assign something to their user meta" – mmm Commented Jul 26, 2024 at 2:20
- I still need to figure out how to remove previously assigned numbers from the 1-10 and then have it assign a random number from the unused numbers. – LBF Commented Jul 26, 2024 at 3:12
1 Answer
Reset to default 0All right, I figured it out. But if someone wants to suggest something better, please feel free and I will mark it correct.
// array of previously-assigned numbers (get this by looping through the registered users -not shown)
$numbers_already_taken = array(3,8,1);
// get all the numbers between 1-10
$numbers = range(1, 10);
// shuffle the numbers so they are in random order
shuffle($numbers);
// set up variable to hold this user's draw #
$draw = '';
// loop through the available numbers
foreach ($numbers as $number) {
// ignore numbers that have already been assigned
if (!in_array($number, $numbers_already_taken)) {
// assign the first matching draw number (break stops it)
$draw = $number;
break;
}
}
echo 'Your Draw Number: ' . $draw;
// save $draw to the user's profile or booking (not shown)
本文标签: phpGenerate random number 110 upon registration without repeat
版权声明:本文标题:php - Generate random number 1-10 upon registration without repeat 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299872a1930613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论