【问题标题】:PHP Function that Counts String Length and Charges Price Based on # of Characters计算字符串长度并根据字符数收费的 PHP 函数
【发布时间】:2013-08-23 16:54:11
【问题描述】:

我有一个 OpenCart VQMod,它目前按字符计算字符串长度和费用。它工作得很好,但我需要它按照以下规则收费:

30-45 个字符:$8.50

超过 46 个字符:$12.00

编辑: 到目前为止,这个模块将字符串长度乘以每个字符的固定价格,但我需要它只收取 30-45 个字符的固定价格 8.50 美元,或 46 个以上字符的 12 美元。谁能帮我修改以下PHP?我在这里粘贴整个文件。非常感谢您到目前为止的回复。我非常感谢社区的帮助。

编辑2:删除了不必要的代码,只显示字符串长度药水。

                    //Q: Option Price By Character
                    $optprice = '';
                    $optprefix = '';
                    if ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea') {
                            if (strlen($option_value)) {
                                $optprice = (strlen($option_value) * $option_query->row['price_per_char']);
                                $optprefix = '+';
                                $option_price += $optprice;

【问题讨论】:

  • 你的代码有什么具体问题?
  • @StephenTG,我需要逻辑方面的帮助,特别是这部分://Q: Option Price By Character $optprice = ''; $optprefix = ''; if ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea') { if (strlen($option_value)) { $optprice = (strlen ($option_value) * $option_query->row['price_per_char']); $optprefix = '+'; $option_price += $optprice; } }

标签: php count character opencart strlen


【解决方案1】:
if ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea') {
    if (strlen($option_value)) {
        // LumberJack's new code
        $string_length = strlen($option_value);
        if($string_length >= 30 && $string_length <= 45) 
        { $optprice = 8.5; }
        else if($string_length >= 46)
        { $optprice = 12.00; }
        else {
        // end my new code
          $optprice = (strlen($option_value) * $option_query->row['price_per_char']);
        } // I moved this up two lines
        $optprefix = '+';
        $option_price += $optprice;
    } 
}

【讨论】:

    【解决方案2】:

    首先找出哪个是最大的数字。在这种情况下,它是 45。

    $price = 8.50;
    for(i=1;i<45;i--){
    
       echo i - $price.'<br/>';
       if(i < $price){
           break;
        }     
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多