<?php
if(!$_GET['reset'] && $_GET['height'] > 0){ // リセットボタンが押されていない かつ 体重が正の値
$age = $_GET['age'];
$sex = $_GET['sex'];
$weight = $_GET['weight'];
$height = $_GET['height'];
if($height != 0){
$bmi = $weight/(($height/100.0)*($height/100.0));
}
/************BMI条件分岐始まり☆*********/
if($sex == 'male'){
$lower = 20;
$upper = 25;
}elseif($age < 40){
$lower = 18;
$upper = 23;
}else{
$lower = 19;
$upper = 24;
}
if($bmi < $bmi){
$judgement = '痩せている';
}elseif($bmi < $bmi){
$judgement = '標準';
}else{
$judgement = '肥満';
}
/************BMI条件分岐終わり***********/
$stdbmi = ($upper + $lower)/2;
$standard_weight = ($height/100)*($height/100)*$stdbmi; //標準体重 = 身長(m)^2*標準BMI
$subtraction = $weight - $standard_weight;
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1.0">
<title>BMIチェック</title>
<style>
table,th,td{
border:1px solid black;
}
table{
border-collapse:collapse;
}
</style>
</head>
<body>
<h1>BMIチェック</h1>
<form action="" method="GET">
<?php
if($_GET['submit'] && ($height <= 0 || $weight <= 0 || $age <= 0)){
print ('<p>間違った値が入力されているか、空欄があります</p>');
}
?>
<table>
<tr>
<th>性別</th><td><input type="radio" name="sex" value="male" <?php if($sex == 'male') print 'checked';?>>男
<input type="radio" name="sex" value="female" <?php if($sex == 'female') print 'checked';?>>女</td>
</tr><tr>
<th>年齢</th><td><input type="text" size="5" name="age" value="<?php print $age;?>">歳</td>
</tr><tr>
<th>身長</th><td><input type="text" size="5" name="height" value="<?php print $height;?>">cm</td>
</tr><tr>
<th>体重</th><td><input type="text" size="5" name="weight" value="<?php print $weight;?>">kg</td>
</tr>
<?php
if(!$_GET['reset'] && $_GET['height'] > 0){
?>
<tr>
<th>BMI</th><td><?php print (int)($bmi*10)/10;?></td>
</tr><tr>
<th>標準体重</th><td><?php print (int)($standard_weight*10)/10 ;?></td>
</tr><tr>
<th>標準体重比</th><td><?php print (int)(($subtraction/$standard_weight)*1000)/10 ;?>%</td>
</tr><tr>
<th>判定</th><td><?php print $judgement;?></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="result" value="結果を見る"><input type="submit" name="reset" value="リセット">
</form>
</body>
</html>