首页 > 解决方案 > PHP - 客户选择 X 数量 - 想要从库存和电子邮件中获取 X 数量

问题描述

所以我已经尝试了一段时间,似乎无法找到解决方案。

我有一个网站,销售数字商品。

当客户结帐时,它会询问数量。

我想这样当他们输入例如数量 5 时,我希望它从数据库中抓取 5 只股票并将所有股票发送到他们的电子邮件。

我需要让它循环并从assignedProduct 是产品ID 的库存表中获取x 数量。

目前,我的代码只获取数量而不是从我的库存数据库中获取不同的行。我如何让它循环股票?

目前显示这个

我当前的代码是(使用区块链支付处理器):

<?php

    $smtp_btc = $pdo->prepare('SELECT * FROM `productitems` WHERE `avaliable` = :avaliable AND `assignedProduct` = :assignedProduct');
    $smtp_btc->execute(array(':avaliable' => '0', ':assignedProduct' => $product_id));
    $query = $smtp_btc->fetchAll();

    // Select all from stock where avaliable = true and assignedProduct is the main productID
    // Then make it 

    // Foreach loop here
    $i = 0;
    $maxiterations = $quantity - 1;
    $message = '';

    foreach($query as $row_product) {
        while($i <= $maxiterations) {
            $i++;
            $product_link_stock = $row_product['code'];
            $stock_id_stock = $row_product['id'];

            $message = "<tr style='background: #eee;'><td><strong>Email/Username:Pass(".$i."):</strong> </td><td>$product_link_stock</td></tr>";
            echo $message.'<br>';
        }
    }


?>

标签: phpforeachblockchainpaypal-ipn

解决方案


所以事实证明我需要在我的 while 循环内进行查询,以便它可以更改它为每个数量选择的行。

这是我的工作代码:

            $i = 1;
            $maxiterations = $quantity;

            while($i <= $maxiterations) {
                $smtp_btc = $pdo->prepare("SELECT * FROM `productitems` WHERE `avaliable` = :avaliable AND `assignedProduct` = :assignedProduct LIMIT $maxiterations");
                $smtp_btc->execute(array(':avaliable' => '0', ':assignedProduct' => $product_id));
                $query = $smtp_btc->fetchAll();

                foreach($query as $row_product) {
                    $product_link_stock = $row_product['code'];
                    $stock_id_stock = $row_product['id'];

                    $message .= "<tr style='background: #eee;'><td><strong>Email/Username:Pass(".$i."):</strong> </td><td>$product_link_stock</td></tr>";
                    // Update stock foreach stock product they require (quantity depends on this)
                    $updateStock = $pdo->prepare('UPDATE `productitems` SET `avaliable` = :avaliable WHERE `id` = :id');
                    $updateStock->execute(array(':avaliable' => '1', ':id' => $stock_id_stock));
                    $i++;
                }
                $i++;
            }

推荐阅读