循环只添加一个结果

时间:2014-04-11 03:59:07

标签: for-loop

情况:

对于每位客户,进行购买,从卖家中减去库存,向卖家帐户添加资金。

    // for each customer
        for ($c = 1; $c <= $maxCustomers; $c++) {

            // decide what to buy
            $willBuy = rand('1','7');

            // buy indica
            if ($willBuy == '1') {

                // how many eights are they going to buy..
                $willSpend = rand('1', '4');

                if ($indicaPrice < $averageIndicaPrice) {

                    if ($indicaAvailable > $willSpend) {
                        // update products
                        $newIndica = $indicaAvailable - $willSpend;
                        update_user_meta($sellerID, 'Indica', $newIndica);  

                        // pay for purchase
                        $newCash = $cash + $indicaPrice;
                        update_user_meta($sellerID, 'cashOnHand', $newCash);    

                        echo "<tr>";
                        echo "<td>#".$c."</td>";
                        if ($willSpend > "1") { echo "<td>".$willSpend." 8th's of Indica Flowers</td>"; }
                        else { echo "<td>".$willSpend." 8th of Indica Flowers</td>"; }
                        echo "<td>$".$indicaPrice * $willSpend."</td>";
                        echo "</tr>";
                    }
                    else {
                        echo "<tr>";
                        echo "<td>#".$c."</td>";
                        echo "<td>Could not find anything they liked. No sale.</td>";
                        echo "<td>$0</td>";
                        echo "</tr>";
                    }
                }

            }
     }

问题#1:在“更新产品”部分,它仅更新1个循环结果。如果有多个客户购买'indica',它只更新1个结果的indica meta值,而不是每个结果更新(即,如果2个人每个购买3个标记,则只减去3个标记而不是6个)。

问题#2:同样的事情发生在“购买付款”部分。它只更新一次购买的现金,而不是每次购买。

1 个答案:

答案 0 :(得分:0)

你永远不会改变$indicaAvailable你呢?你不应该根据售出的东西更新吗?