折扣价格和显示后期价值时遇到问题。

时间:2018-04-29 15:59:37

标签: php

所以我想按$存储在$ discountPercent变量中的百分比来折扣$ cost变量。我已经尝试了多种方法尝试这样做,但没有工作。此外,我在索引页面上有一个选择菜单,每当我选择一个国家时,它在我的“displayinfo”页面上显示为1 ...我很困惑它让我疯狂

<?php
  include 'includes/connect.php';

  if(isset($_POST['submit'])) {

    $name = $_POST['f_name']." ".$_POST['l_name'];
    $sl1 = $_POST['sl1'];
    $sl2 = $_POST['sl2'];
    $country = $_POST['country'];
    $county = $_POST['county'];
    $team = $_POST['team'];
    $size = $_POST['size'];
    $quantity = $_POST['quantity'];

    if(isset($_POST['town'])) {
      $town = $_POST['town'];
    } else {
      $town = "N/A";
    }


    function get_cost($size, $quantity) {

      if($size == "S" || $size = "XS") {
        $jerseyCost = 45;
      } else if($size == "M" || $size = "L" || $size = "XL") {
        $jerseyCost = 50;
      }

      $cost = $jerseyCost * $quantity;
      return $cost;
    }

    $cost = get_cost($size, $quantity);

    function check_discount_percent($cost) {
      if($cost > 100 && !($cost > 250)) {
        return 10;
      }
      else if($cost > 250) {
        return 25;
      } else {
        return "Not applicable to receive discount.";
      }
    }

    $discountPercent = check_discount_percent($cost);

    function get_total_cost($cost, $discountPercent) {
      if($discountPercent === 0){
        $discountedPrice = $cost / 100 * $discountPercent;
        return $cost - $discountedPrice;
      } else {
        return $cost;
      }
    }

    $totalCost = get_total_cost($cost, $discountPercent);

    if($country == "Ireland") {
      $deliveryTime = "3 days";
    } else if($country == "United States" ) {
      $deliveryTime = "3 weeks";
    } else if($country = "United Kingdom" || "France" || "Italy" || "Spain" || "Germany" || "Portugal" || "Switzerland" || "Denmark" || "Iceland") {
      $deliveryTime = "1 week";
    } else {
      $deliveryTime = "2 - 6 weeks";
    }

  }
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax Project | Cian Tiernan</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
    <header>
      <h3> Ajax Project </h3>
      <h4 id="home"><a href="index.php">Home</a></h4>
    </header>
    <div class="wrapper">
      <h2 class="details"> Your order details!</h2>

      <h3> Name </h3>
      <p><?=$name?></p>

      <h3> Street Line 1 </h3>
      <p><?=$sl1?></p>

      <h3> Street Line 2 </h3>
      <p><?=$sl2?></p>

      <h3> Country </h3>
      <p><?=$country?></p>

      <h3> County </h3>
      <p><?=$county?></p>

      <h3> Town </h3>
      <p><?=$town?></p>

      <h3> Team </h3>
      <p><?=$team?></p>

      <h3> Size </h3>
      <p><?=$size?></p>

      <h3> Quantity </h3>
      <p><?=$quantity?></p>

      <h3> Items Cost </h3>
      <p> €<?=$cost?></p>

      <h3> Discount Percentage </h3>
      <p><?=$discountPercent?>%</p>

      <h3> Total Cost </h3>
      <p>€<?=$totalCost?></p>

      <div id="map">
        <iframe height="300px" width="45%" src="https://www.google.com/maps?z=4&f=q&output=embed&q=<?=$county?>"></iframe>
        <h4>Delivery Time</h4>
        <p><?=$deliveryTime?></p>
      </div>

      <a class="top" href="#"><h2>Back to top</h2></a>

    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

在函数get_total_cost中,只有在$ discountPercent为零时才应用折扣,这没有意义。只有$ discountPercent&gt;才能应用它或者,或者,只要总是应用折扣,即使它是0%。如下所示:

function get_total_cost($cost, $discountPercent) {
  $discountedPrice = $cost / 100 * $discountPercent;
  return $cost - $discountedPrice;
}