2012-03-10 49 views
-2

我是相當新的,我需要PHP的幫助,我正在做一個BMI計算器來計算一個人的BMI,通過詢問體重和身高以及測量系統的優先級(公制/英制),並評估用戶的體重指數(即BMI低於正常,正常,和正常以上),現在我的問題是,輸出不斷給我兩個評估結果是這樣的:(請參閱下面的img attch'd。)![見img這裏] [1]我不知道爲什麼它給了我兩個「你的BMI超過正常水平」,只有一個。請參閱鏈接,以獲得更好的理解什麼,我想說:http://imgur.com/EHXQjPHP編碼的幫助計算器

這裏是我的代碼:

<?php 

$weight = $_POST["txtWeight"]; 
$height = $_POST["txtHeight"]; 
$unit = $_POST["optUnit"]; 
$metric = ($weight/($height * $height)); 
$imperial = (($weight * 703)/($height * $height)); 

if ($unit == "metric") 
    echo "A height of $height Meters and a weight of $weight Kilograms = " .round($metric,1). " BMI.<br />"; 

     { 
     if($metric <= 18.5) 

      echo " Your BMI is below normal"; 

     else if ($metric >= 18.5 and $metric <= 24.9) 

      echo " Your BMI is normal"; 

     else if($metric >= 25) 

      echo " Your BMI is above normal"; 
     }; 

if ($unit == "imperial") 
    echo "A height of $height Inches and a weight of $weight Pounds = " .round($imperial,1). " BMI.<br />"; 
     { 
     if($imperial <= 18.5) 

      echo "Your BMI is below normal"; 

     else if ($imperial >= 18.5 and $imperial <= 24.9) 

     echo "Your BMI is normal"; 

     else if($imperial >= 25) 

      echo "Your BMI is above normal"; 
     };    

>

+0

此圖像顯示502 - 錯誤的網關。 – Milap 2012-03-10 04:15:57

+0

而你的問題是? – 2012-03-10 04:21:35

回答

1

我的猜測是你的括號放錯了位置。試試這個:

<?php 

$weight = $_POST["txtWeight"]; 
$height = $_POST["txtHeight"]; 
$unit = $_POST["optUnit"]; 
$metric = ($weight/($height * $height)); 
$imperial = (($weight * 703)/($height * $height)); 

if ($unit == "metric") { 
    echo "A height of $height Meters and a weight of $weight Kilograms = " . round($metric, 1) . " BMI.<br />"; 

    if ($metric <= 18.5) { 
    echo "Your BMI is below normal"; 
    } else if ($imperial >= 18.5 && $imperial <= 24.9) { 
    echo "Your BMI is normal"; 
    } else { 
    echo "Your BMI is above normal"; 
    } 
} 

if ($unit == "imperial") { 
    echo "A height of $height Inches and a weight of $weight Pounds = " . round($imperial, 1) . " BMI.<br />"; 

    if ($imperial <= 18.5) { 
    echo "Your BMI is below normal"; 
    } else if ($imperial >= 18.5 && $imperial <= 24.9) { 
    echo "Your BMI is normal"; 
    } else { 
    echo "Your BMI is above normal"; 
    } 
}   

?> 

一個更好的方法是凝結在重複代碼:

<?php 

$weight = $_POST["txtWeight"]; 
$height = $_POST["txtHeight"]; 
$unit = $_POST["optUnit"]; 

if ($unit == "metric") { 
    echo "A height of $height Meters and a weight of $weight Kilograms = " . round($metric, 1) . " BMI.<br />"; 
    $bmi = ($weight/($height * $height)); 
} else if ($unit == "imperial") { 
    echo "A height of $height Inches and a weight of $weight Pounds = " . round($imperial, 1) . " BMI.<br />"; 
    $bmi = (($weight * 703)/($height * $height)); 
} 

if (isset($bmi)) { 
    if ($bmi <= 18.5) { 
    echo "Your BMI is below normal"; 
    } else if ($bmi >= 18.5 && $bmi <= 24.9) { 
    echo "Your BMI is normal"; 
    } else { 
    echo "Your BMI is above normal"; 
    } 
} 

?> 
2

你不封閉你的if語句?如此多的代碼正在運行,你不會期望。