开发者

php script error gving wrong output

I am having a little trouble regarding working out 20% of a total when testing I select it so the value of the 'basket' is 60, I would like it to display 60 (which it does) but then it should work out 60 + 20% which should give the reading 72 but insted it gives 144 I was wondering if you peeps could help

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Feet First</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <!-- The container holds the entire website everything else is 'contained' within the container -->
    <div class="container">
      <div class="header">

        <img src="header.png" alt="logo" width="892" height="142" id="logo" />
    </div>
      <!-- The content divis the Secondary layer which contains the content of the webiste such as text and images.  -->
      <div class="content"> 
        <!-- Navi is what hols the navigation links at the top of the page -->
       <div class="navi">
       <!-- this is 开发者_JAVA百科the unordered list for the menu items -->
       <ul id="menu">
       <li><a href="\index.html">Home</a></li>
       <li><a href="\trainers.html">Trainers</a></li>
       <li><a href="\order.html" >Order</a></li>
       <li><a href="\credits.html">Credits</a></li>
       <li></li>
       <li></li>
    </ul>
    </div>

    <div class="content"></div>



        <h1>Thank you for your order!</h1>
    <?php

    // sents the value of the items
      $extraValues = array(
          'Laces' => 5,
          'Shoe Polish' => 10,
          'In-souls' => 15
       );

       $trainerValues = array(
          'Lacoste' => 50,
          'K-Swiss' => 45,
          'Puma' => 59,
          'Converse' => 65
       );

       if(isset($_POST['firstname'])){
          $firstname = $_POST['firstname'];
          echo " $firstname ";
       }

       if(isset($_POST['lastname'])){
        $lastname = $_POST['lastname'];
        echo "$lastname <br />\n";
       }

      if(isset($_POST['add1'])){
        $add1 = $_POST['add1'];
        echo "$add1 <br />\n";
      }

     if(isset($_POST['add2'])){
      $add2 = $_POST['add2'];
      echo "$add2 <br />\n";
     }

      if(isset($_POST['postcode'])){
        $postcode = $_POST['postcode'];
        echo "$postcode <br />\n";
      }

      if(isset($_POST['email'])){
        $email = $_POST['email'];
        echo "Contact Email Address $email <br />\n";
      }

      if(isset($_POST['telephone'])){
      $telephone = $_POST['telephone'];
      echo "Contact Telephone Number $telephone <br />\n";
      }

      if(isset($_POST['contact'])){
      $contact = $_POST['contact'];
      echo "You would like to be contacted by $contact <br />\n";
      }

      if(isset($_POST['trainers'])){
      $trainers = $_POST['trainers'];
      echo "The trainers you would like are $trainers <br />\n";
      }



      if(isset($_POST['extras'])){
      $extras = $_POST['extras'];
      echo "The extras you would like are $extras <br />\n";
      }



       $extraCost = 0;
       $trainerCost= 0;
       $totalCost= 0;
       $totalCostV= 0;

        $extra = $_POST['extras'];

        if (array_key_exists($extra, $extraValues)) {
             $extraCost = (float) $extraValues[$extra]; 
            echo "The cost of your extras are &pound; $extraCost<br />\n";
        } 


        $trainer = $_POST['trainers'];

        if (array_key_exists($trainer, $trainerValues)) { 
            $trainerCost = (float) $trainerValues[$trainer]; 
            echo "The cost of your Trainers are &pound; $trainerCost<br />\n";
        }

        $totalCost = round($extraCost+$trainerCost+$totalCost); 
      echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";


      $totalCostV = round(($extraCost+$trainerCost+$totalCost)*1.20,2); 
      echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";
    ?>
      </div>

      <!-- Holds the foorter information -->
      <div class="footer">
        <p>&#169; 2010 FeetFirst Uk Ltd </p>
        </div>

      </div> 

    </body> 


Because, at the beginning your $totalCost = 0. Then it should run ok with this:

$totalCost = round($extraCost+$trainerCost+$totalCost); 
echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";

$totalCostV = round($totalCost*1.20,2); // this line is edited
echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";

But I think it should look like:

$totalCost = round($extraCost+$trainerCost); // this line is edited
echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";

$totalCostV = round($totalCost*1.20,2); // this line is edited
echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";


$totalCost = 0
...
$totalCost = round($extraCost+$trainerCost+$totalCost);
=> 60
...

$totalCostV = round(($extraCost+$trainerCost+$totalCost)*1.20,2);
            = round(($extraCost+$trainerCost+$extraCost+$trainerCost)*1.20,2);
            = round(($extraCost+$trainerCost)*2*1.20,2);
            = 144

I think you have an exta +$totalCost in $totalCostV


Might be a conversion problem. Sounds like it calculates *2 instead of *1.2. You could try putting a (float) conversion before those calucations.

Because JS floats are quite inaccurate it might also be an idea to calculate everything in cents and then just divide by 100 when outputting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜