嗨,我正在研究一种抽奖系统,它将 100 万个随机数分成 x 数量的票,例如 100 万个随机数到 10,000 张票。
每张票都是数据库中的一行,然后我们有另一个表票号,我需要在其中为每张票提供 100 个数字,它们与票号相关。
所以目前这是我的代码:
//Amount of the 1 million tickets divided to the tickets
$numbersPerTickets = $_POST['numbersPerTicket'];
//The total cost of the property
$propertyPrice = $_POST['propertyPrice'];
//The total amount of tickets
$totalTickets = NUMBER_CIELING / $numbersPerTickets;
//The ticket price
$ticketPrice = $propertyPrice / $totalTickets;
//Generate array with random numbers up to 999,999
$randomTicketNumbers = createTicketNumbers();
//Creation loop counter
$ticketCreationCount = 1;
//Loop and create each ticket
while($ticketCreationCount <= $totalTickets)
{
//Create a padded ticket number
$ticketNumber = str_pad($ticketCreationCount, 6, 0, STR_PAD_LEFT);
$query = '
INSERT INTO tickets(
propertyID,
ticketNumber,
price
)
VALUES(
"'.$propertyID.'",
"'.$ticketNumber.'",
"'.$ticketPrice.'"
)
';
$db->query($query);
//Get the ID of the inserted ticket to use to insert the ticket numbers
$ticketID = $db->insert_id;
$loopBreak = $numbersPerTickets;
$addedNumberCount = 1;
foreach($randomTicketNumbers as $key => $value)
{
$query = '
INSERT INTO ticketNumbers(
ticketID,
number
)
VALUES(
"'.$ticketID.'",
"'.$value.'"
)
';
$db->query($query);
unset($randomTicketNumbers[$key]);
if($addedNumberCount == $loopBreak){
break;
}else{
$addedNumberCount++;
}
}
$ticketCreationCount++;
}
但这不起作用,它添加了适量的票,在测试的情况下是 10,000,但是添加了太多的票号,最终超过了随机票数组中的百万个数字,随机票数组只是一个简单的 1 层数组,包含 100 万个随机排序的数字。