【问题标题】:PHP incremntal Price Calculator based upon quantity基于数量的 PHP 增量价格计算器
【发布时间】:2014-11-26 21:27:11
【问题描述】:

我正在尝试编写一个根据数量计算价格的脚本。

数据库:
pid |产品 |单位 |价格
1 |可乐   |100 | 2
2 |可乐   |100 | 1.8
3 |可乐   |300 | 1.5
4 |可乐   | | 1.1
1 |百事可乐   |100 | 2.2
2 |百事可乐   |50 | 2
3 |百事可乐|  | | .8

Pid 是产品 id,Product 是产品名称/产品代码。 单位为增量单位,或除现有单位的数量外,价格为增量单位的每单位价格。

实际问题:

  • 如果单位为'',或Null,即标识最大累积单位,然后该空列中的价格适用于此。
  • 并非所有产品都具有相同的价格水平。有些可能以 1 值结束(统一费率,在这种情况下,单位将为 null 或 ''。有些可能会尽可能增加。
  • 没有累积列,我无法添加。它不存在,并且现在无法添加,因为产品可能以数百万计(假设​​,使代码对无限产品灵活)
  • 您将得到的只是单位和产品代码的总和。例如。 100 单位可乐,或 200 单位百事可乐。
  • 单位列中的数字包含在内,即小于等于。


我是 For 循环和中断(糟糕的编程)的忠实粉丝,但现在我认为我需要 if 条件或 while 循环,因为我对两者都没有多大信心。

提前谢谢您

注意如果您觉得难以理解问题,那么只需假设所得税计算器,相同或相似的东西 - 最多 x 金额,基础税,然后对于下一个y金额,y税率,对于下一个z金额,z税率超过z,z+税

【问题讨论】:

    标签: php mysql inventory


    【解决方案1】:

    嗯,你当然想先循环遍历产品,然后根据传入的数量计算出总数。像这样吗?

    // Using PostgreSQL as an example here
    $entries = pg_fetch_all(pg_query('SELECT * FROM database ORDER BY prodict, pid ASC'));
    
    // Have the quantities ready;
    $quantities = array(
        'coke' => 1024,
        'pepsi' => 512,
    );
    
    // Prepare an array to hold the total values.
    $totals = array();
    
    // Loop through the entries in the array in a non-conventional way
    while($entry = current($entries)) {
        // Get the name of the product from the array
        $product = $entry['prodict'];
        // And the quantity
        $quantity = $quantities[$product];
        // Prepare a price for this product starting at zero
        $price = 0;
        // Now use a do-while to figure out the price
        do {
            // At this point '$entry' contains information about the pricing for the first 'x' quanitity
            $quantityAtThisPrice = $entry['unit'];
            // Check the price
            $priceForThisUnit = $entry['price'];
            // Check we have any quantity remaining of this product
            if($quantity > 0) {
                // Check if the quantity at this price is null or if that quantity at this price is more than what we have left
                if($quantityAtThisPrice === null || $quantityAtThisPrice > $quantity) {
                    // This means the rest of the quantity is at this price
                    $price += ($quantity * $priceForThisUnit);
                    // No more quantity left to price
                    $quantity = 0;
                }
                // Otherwise we'll add for this unit, and move on to the next
                else {
                    // Add to the price
                    $price += ($quantityAtThisPrice * $priceForThisUnit);
                    // Subtract the quantity we just priced
                    $quantity -= $quantityAtThisPrice;
                }
            }
            // Now fetch the next entry
            $entry = next($entries);
        } while ($entry['prodict'] === $product);
    
        // Add the calculated price to the totals array
        $totals[$product] = $price;
    }
    
    var_dump($totals);
    

    有点得意忘形,但我认为这应该可行。

    【讨论】:

    • 使用 PDO 可能比使用低级 Postgres 驱动程序更简洁。
    • 我肯定会说使用 PDO,但是当我写第一行时,我希望这个答案既快又脏。
    • Scopey,谢谢 - 拯救了我的一天。为我的产品目的定制它。现在正在努力制作课程
    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多