PHP BMI計算機

```php:BMICalculator.php
<?php
session_start ();
if (isset ( $_SESSION ['result'] )) {
    $result = $_SESSION ['result'];
    $body_type = $_SESSION ['body_type'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">
 
 
    <title>BMI計算機</title>
<style type="text/css">
div.img-move a:hover {
 position: relative;
 top: -1px;
 left: -1px;
}
div.img-move a:active {
 position: relative;
 top: 5px;
 left: 5px;
}
</style>
</head>
<body>
    <font color="#ff9933">
        <h1>BMI計算機</h1>
    </font>
    <form action="BMICalculator_output.php" method="post">
        <p>
            身長:
            <textarea rows="1" cols="3" name="height"></textarea>
            cm
        </p>
        <p>
            体重:
            <textarea rows="1" cols="2" name="weight"></textarea>
            kg
        </p>
        <input type="submit" value="BMI値計算" name="button1"> <input
            type="submit" style="margin-left: 10px;" value="1日必要摂取カロリー計算"
            name="button2"><input type="submit" style="margin-left: 10px;"
                value="リセット" name="button3">
                    <p>
                        <input type="submit" value="一日に必要な食事量目安" name="button4">
 
                    </p>
    </form>
                        <div class="img-move">
                    <a href="test08_mailform_input.php"><img src="contact_us.png" alt="contactus" /></a>
                    </div>
 
 
    <p>
        <b><font color="#4682B4">
        <?php
        if (isset ( $_SESSION ['result'] )) {
            echo 'あなたのBMI値は<font color="red">', $result, '</font>です';
            echo '</br><font color="red">', $body_type, '</font>';
        }
        ?>
        <?php
        if (isset ( $_SESSION ['weight1'] )) {
            $weight1 = $_SESSION ['weight1'];
            function getCalorie($weight1) {
                $resultCalorie = $weight1 * 25;
                echo '</br>一日の必要摂取カロリーは<font color="red">', $resultCalorie, 'kcal</font>です';
            }
            getCalorie ( $weight1 );
        }
        ?>
</font></b>
<?php
 
if (isset ( $_SESSION ['picture'] )) {
    $picture = $_SESSION ['picture'];
    echo $picture;
}
?>
    </p>
 
</body>
</html>
```

```php:BMICalculator_output.php
<?php
session_start ();
 
$height = $_POST ['height'];
$weight = $_POST ['weight'];
if (isset ( $_POST ['button1'] ) && ! empty ( $height ) && ! empty ( $weight )) {
    function getBmi($weight, $height) {
        $height = $height * 0.01;
        $bmi = $weight / ($height * $height);
        return floor ( $bmi );
    }
 
    $result = getBmi ( $weight, $height );
    $_SESSION ['result'] = $result;
    switch ($result) {
        case $result >= 20 && $result <= 21 :
            $body_type = '少し太っています';
            break;
        case $result >= 18 && $result <= 19 :
            $body_type = 'モデル並(理想的)です';
            break;
        case $result <= 17 :
            $body_type = '痩せています';
            break;
 
        default :
            $body_type = '太っています';
            break;
    }
    $_SESSION ['body_type'] = $body_type;
 
    unset ( $_SESSION ['weight1'] );
    unset ( $_SESSION ['picture'] );
}
if (isset ( $_POST ['button2'] ) && ! empty ( $weight )) {
    $_SESSION ['weight1'] = $weight;
    unset ( $_SESSION ['result'] );
    unset ( $_SESSION ['picture'] );
}
 
if (isset ( $_POST ['button3'] )) {
    $_SESSION = array ();
    session_destroy ();
}
 
if (isset ( $_POST ['button4'] ) && ! isset ( $_SESSION ['picture'] )) {
    $picture = '<img src="guide_title_cut08.gif">';
    $_SESSION ['picture'] = $picture;
} else if (isset ( $_POST ['button4'] ) && isset ( $_SESSION ['picture'] )) {
    unset ( $_SESSION ['picture'] );
}
 
header ( 'Location:BMICalculator.php' );
?>
```