2011-03-28 134 views
0

我在測試時遇到總數的20%時遇到了一些麻煩,所以我選擇「籃子」的值爲60,我希望它顯示60(它確實),但是它應該工作了60 + 20%,這應該給閱讀72,但insted的它給144我在想,如果你偷看能幫助php腳本錯誤gving錯誤輸出

<!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 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> 

回答

1

因爲,一開始你的$ TOTALCOST = 0,那麼就應該有這個運行正常:

$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"; 

但我認爲它應該是這樣的:

$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"; 
+0

那就是:D謝謝! – Chris 2011-03-28 09:44:52

0
$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 

我想你在$totalCostV

有EXTA +$totalCost
+0

沒有它們都是相同,沒有額外的東西 – Chris 2011-03-28 09:40:04

+0

'code' $ totalCost = round($ extraCost + $ trainerCost + $ totalCost); echo「你的培訓師的費用是(不含20%稅)£ $ totalCost
\ n」; $ totalCostV = round(($ extraCost + $ trainerCost + $ totalCost)* 1.20,2); echo「你的培訓師的成本是(包括20%的稅)£ $ totalCostV
\ n」; – Chris 2011-03-28 09:41:15

+0

是的,但如果'$ totalCost = round($ extraCost + $ trainerCost + $ totalCost)= 60'然後第二個'$ extraCost + $ trainerCost + $ totalCost將等於120' – manji 2011-03-28 09:45:25

0

可能是一個轉換問題。聽起來像它計算* 2而不是* 1.2。 您可以嘗試在這些卡路里之前進行(浮動)轉換。

因爲JS浮點數是相當不準確的,它可能也是一個想法,以美分計算一切,然後在輸出時除以100。