douzhoubing2805 2018-09-12 20:24
浏览 31

PHP如何通过api收到后调整价格

Background - The page connects to an api to get some information. When it displays on the page I need to increase the price by 1.25% for the summer season.

This the info from the API: (this works)

<?php
$types = "a,b,c,camper-van";
$vehicles = json_decode(file_get_contents("https://api.outdoorsy.co/v0/rentals?owner_id=24019&limit=1000&full=true&type=" . $types), true);
$seasons = json_decode(file_get_contents("https://api.outdoorsy.co/v0/seasons?owner_id=24019&limit=1000"), true);
usort($vehicles, "custom_sort");
foreach ($vehicles as $vehicle):
?>

This is the displayed section of the page:

<p class="fleet-list-item_rates">
   <strong>Sept. 15th – May 14th</strong>:&nbsp;&nbsp; <?php print(money_format('%.2n', $vehicle["price"]["day"] / 100)) ?> / day &nbsp;&nbsp;&nbsp;<em>*7 Days or More 14% Discount</em>
   <?php
   foreach($seasons as $season):
       $price = find_price_within_season($season, $vehicle["id"]);
       if ($price):
         $day = $price["day"] / 100.0;
       else:
         $day = ($vehicle["price"]["day"] / 100.0) * ($season["adjust_price_percent"] / 100);
       endif;
       ?>
       <br>
       <strong><?php print(friendly_date($season["from"])) ?> - <?php print(friendly_date($season["to"])) ?></strong>:&nbsp;&nbsp; <?php print(money_format('%.2n', $day)) ?> / day
<?php endforeach; ?>
</p>

This is my edit to add the addition 1.25%

    <p class="fleet-list-item_rates">
       <strong>May 14th - Sept. 15th</strong>:&nbsp;&nbsp; <?php print(money_format('%.2n', $vehicle["price"]["day"] / 100)) ?> / day &nbsp;&nbsp;&nbsp;<em>*7 Days or More 10% Discount</em>
<?php
    $percentagechange = 1.25;
    $numberToAdd = ($price / 100) * $percentagechange;
    $newNumber = $price + $numberToAdd;
    $newNumber = ["newNumber"];
    foreach($seasons as $season):
    $price = find_price_within_season($season, $vehicle["id"]);
    if ($price):
        $day = $price["day"] / 100.0;
    else:
        $day = ($vehicle["price"]["day"] / 100.0) * ($season["newNumber"] / 100);
    endif;
?>
    <br>
    <strong><?php print(friendly_date($season["from"])) ?> - <?php print(friendly_date($season["to"])) ?></strong>:&nbsp;&nbsp; <?php print(money_format('%.2n', $day)) ?> / day
<?php endforeach; ?>
</p>

Just at a loss on how to increase the value by 1.25%.

  • 写回答

1条回答 默认 最新

  • duandu1915 2018-09-12 20:36
    关注

    Seems like you were calculating the percentage increase before you had the actual price.

    In terms of how to do the calculation:

    $percentagechange = 1.25;
    $numberToAdd = ($price * $percentagechange) / 100;
    $newNumber = $price + $numberToAdd;
    // Get rid of the following. It is overwriting your calculated number.
    //$newNumber = ["newNumber"];
    

    Or

    $newNumber = $price * 1.0125;
    

    How to change your code:

    if ($price):
        $day = $price["day"] / 100.0;
    else:
        $day = ($vehicle["price"]["day"] / 100.0) * ($season["newNumber"] / 100);
    endif;
    // At this point $day contains the price per day
    // Increase price by 1.25%
    $day *= 1.0125;
    ?>
    <br>
    <strong>
      <?= friendly_date($season["from"]) ?> - <?= friendly_date($season["to"]) ?>
    </strong>
      :&nbsp;&nbsp; <?= money_format('%.2n', $day) ?> / day
    <?php endforeach; ?>
    

    Note that I changed the code to use <?= instead of <?php print. They do the same thing except one is shorter than the other.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line