【问题标题】:Creating Automatic price chart for word ranges in php在php中为单词范围创建自动价格图表
【发布时间】:2022-08-14 00:37:06
【问题描述】:

我想在 php 中创建一个自动价格图表。在下面的屏幕截图中,我将输入字数范围的价格,然后单击保存,它应该以我将输入字数的方式创建一个价格图表,它会给我计算出的价格。例如 50 字 50 美元和 500 字 150 美元,那么它应该以某种方式显示计算价格,如果我写 51 字,那么价格不应该低于 50 美元。

下面是我的代码,它不能 100% 正确工作,但我需要一些帮助。

                if($words >= 1 AND $words <= 5){
                    $given_price = $price_1;
                    $given_words = $words_1;
                    $per_word = $given_price / $given_words;
                }
                if($words > 5 AND $words <= 50){
                    $given_price = $price_2;
                    $given_words = $words_2;
                    $per_word = $given_price / $given_words;
                }
                elseif($words > 50 AND $words <= 500){
                    $given_price = $price_3;
                    $given_words = $words_3;
                    $per_word = $given_price / $given_words;
                }
                elseif($words > 500 AND $words <= 5000){
                    $given_price = $price_4;
                    $given_words = $words_4;
                    $per_word = $given_price / $given_words;
                }
                elseif($words > 5000 AND $words <= 50000){
                    $given_price = $price_5;
                    $given_words = $words_5;
                    $per_word = $given_price / $given_words;
                }
                elseif($words > 50000){
                    $given_price = $price_5;
                    $given_words = $words_5;
                    $per_word = $given_price / $given_words;
                }
                $total_price = $words * $per_word;


$per_word and $per_price are the values of the boxes in below image

    标签: php


    【解决方案1】:

    我们应该将价格放入一个数组或其他东西中,这样我们就可以减少样板文件,如果需要,价格的数量也可以很容易地改变。

    $prices = array(
        5 => 5,
        50 => 50,
        500 => 150,
        5000 => 4500,
        50000 => 5500
    );
    
    $words_rem = $words;
    $total_price = 0;
    foreach ($prices as $given_words => $given_price) {
        $given_words -= $words - $words_rem;
        $given_price -= $total_price;
        $cur_words = min($words_rem, $given_words);
        $per_word = $given_price / $given_words;
        $total_price += $cur_words * $per_word;
        $words_rem -= $cur_words;
    }
    $total_price += $words_rem * end($prices) / array_key_last($prices);
    

    【讨论】:

    • 实际上,每个用户都会为单词范围给出自己的价格。用户 A 将为 50 个单词支付 10 美元。用户 B 将给 50 美元 50 字。用户 A 将为 500 字范围提供 150 美元,用户 B 将为 500 字范围提供 200 美元。
    • 这也不正确。我想要这样,例如,如果 50 个字在 50 美元上,500 个字在 150 美元上。如果我输入 51 个单词,那么价格会从 50 美元略微增加,可能以美分为单位,如果我想要 500 个单词,价格应该在 150 美元以内。
    • 例如,我写 225 个字,那么价格应该是 88.87 美元,如果我需要 300 个字,那么价格应该是 101.01 美元,如果我需要 400 个字,那么价格应该是 127.10 美元,500 个字的价格应该是 150 美元。这只是一个例子,但应该这样工作
    • @NaveedAli 我编辑了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2014-12-16
    • 1970-01-01
    • 2023-03-30
    • 2012-09-02
    • 1970-01-01
    相关资源
    最近更新 更多