简体   繁体   English

PHP号码票系统?

[英]PHP Number Ticket System?

So, I'm trying to make a ticket system. 因此,我正在尝试制作票务系统。 So a users will be assigned 1000 tickets each, I will then generate a number from 1 to 5000, I then need it to select the user. 因此,将为每个用户分配1000张票证,然后我将生成一个从1到5000的数字,然后需要它来选择用户。

The way I have done it was made an array then looped 5000 times and assigned each user a ticket, however this doesn't work with really big numbers like 5,000,000. 我这样做的方法是将其制成一个数组,然后循环5000次并为每个用户分配一张票证,但是,这对于5,000,000这样的大数字来说是行不通的。 So I'm trying to think of the best way to do this and I'm unsure how. 因此,我正在尝试考虑执行此操作的最佳方法,但不确定如何操作。

Any advice? 有什么建议吗?

    $users = array();
$ticketNum = 0;
$result = $MySQL->query("SELECT * FROM  `users` ");
while($row = $result->fetch_assoc()){
    $i = 0;
    while($i < $row['points']){
        $i++;
        $ticketNum++;
        $ar = array("ticketNum" => $ticketNum, "username" => $row['username']);
        array_push($users, $ar);

    }

}

$winningTicket = 2500;

foreach($players as $ar){
    if($winningTicket == $ar['ticketNum']){
        //winner
    }
}

I'll need more code to suggest a perfect solution, and also know why you want to assign 1000 tickets to a person? 我将需要更多代码来提出理想的解决方案,并且还知道为什么要为一个人分配1000张票?

But you could assign a from-to key instead of looping ALL of the numbers through, like 但是您可以分配一个从-到键,而不是循环遍历所有数字,例如

$users[$userNumber]["from"] = 1;
$users[$userNumber]["to"] = 6;

$findNumber = rand(1,6);

$foundUser = false;
foreach($users as $userNumber => $user) {
  if ($user["from"] <= $findNumber && $user["to"] >= $findNumber) {
    $foundUser = $user;
    break;
  }
}
if ($foundUser) {
   print "Hooray, someone just got a ticket.";
}

but it does sound like a job you would solve in a different manner, maybe through your database. 但这听起来确实像您可以通过数据库以其他方式解决的工作。

Edit: I wrote the above before you added your code example, In your case I would probably do the following. 编辑:在您添加代码示例之前,我已经写了以上内容,就您而言,我可能会执行以下操作。

$result = $MySQL->query("SELECT username FROM users ORDER BY RAND() LIMIT 1");

$winningUser = $result->fetch_assoc();

print "We got a winner: ".$winningUser["username"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM