duanrui3480 2012-07-07 14:58
浏览 47
已采纳

在MySQL + PHP中划分行之间的数字

Hi I am working on a kind of raffle system which divides 1 million random numbers into an x amount of tickets, e.g. 1 million random numbers to 10,000 tickets.

Each ticket is a row in a database, we then have another table ticket numbers in which i need to give 100 numbers to each ticket they are related by the ticket id.

So at the moment this is my code:

//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++;

}

But this isn't working it adds the right amount of tickets, which in the case for testing is 10,000 but then adds far too many ticket numbers, it ends up exceeding the million numbers in the random tickets array, The random tickets array is just a simple 1 tier array with 1 million numbers sorted randomly.

  • 写回答

1条回答 默认 最新

  • dsxml2169 2012-07-07 15:11
    关注

    Change the foreach loop to for:

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

    Guaranteed to only give you $numbersPerTickets iterations and removes the complexity of the iterator++/break logic.

    Sometimes simple is better.

    Please correct my php! TIA.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?