gpt4 book ai didi

php - MySQL + PHP 中行之间的除法

转载 作者:行者123 更新时间:2023-11-29 08:50:31 28 4
gpt4 key购买 nike

嗨,我正在开发一种抽奖系统,它将 100 万个随机数分成 x 数量的门票,例如100 万个随机数对应 10,000 张门票。

每张票都是数据库中的一行,然后我们有另一个表票号,其中我需要为每张票提供 100 个号码,它们通过票证 ID 相关联。

所以目前这是我的代码:

//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,但随后添加了太多的门票号码,最终超过了随机门票数组中的百万个号码,随机门票array 只是一个简单的 1 层数组,其中有 100 万个随机排序的数字。

最佳答案

将 foreach 循环更改为 for:

for ($i = 1; $i <= $numbersPerTickets; $i++) {
$query = '
INSERT INTO ticketNumbers(
ticketID,
number
)
VALUES(
"'.$ticketID.'",
"'.$randomTicketNumbers[$i].'"
)
';

保证只为您提供 $numbersPerTickets 迭代并消除迭代器++/break 逻辑的复杂性。

有时简单更好。

请更正我的PHP! TIA。

关于php - MySQL + PHP 中行之间的除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376038/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com