BMI Calculator & Personal Weight Guide
Enter your details below to calculate your BMI and view your custom weight matrix.
Enter your details below to calculate your BMI and view your custom weight matrix.
<?php include('includes/header.php'); ?>
<h1>BMI Calculator & Personal Weight Guide</h1>
<p>Enter your details below to calculate your BMI and view your custom weight matrix.</p>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label for="height">Height (in inches, e.g., 69 for 5'9"):</label>
<input type="number" step="0.1" name="height" id="height" required
value="<?php echo isset($_POST['height']) ? htmlspecialchars($_POST['height']) : ''; ?>">
</div>
<div class="form-group">
<label for="weight">Weight (in pounds, e.g., 160):</label>
<input type="number" step="0.1" name="weight" id="weight" required
value="<?php echo isset($_POST['weight']) ? htmlspecialchars($_POST['weight']) : ''; ?>">
</div>
<button type="submit" name="calculate">Calculate BMI</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['calculate'])) {
$height_in = floatval($_POST['height']);
$weight_lbs = floatval($_POST['weight']);
if ($height_in > 0 && $weight_lbs > 0) {
// Core Imperial BMI Logic: (Weight / Height²) * 703
$bmi = ($weight_lbs / ($height_in * $height_in)) * 703;
$bmi = round($bmi, 1);
if ($bmi < 18.5) {
$category = "Underweight";
$css_class = "underweight";
} else if ($bmi >= 18.5 && $bmi < 25.0) {
$category = "Normal Weight";
$css_class = "normal";
} else if ($bmi >= 25.0 && $bmi < 30.0) {
$category = "Overweight";
$css_class = "overweight";
} else {
$category = "Obese";
$css_class = "obese";
}
echo "<div class='result-box {$css_class}'>";
echo "<h2>Your Results</h2>";
echo "<p>Your BMI is <strong>{$bmi}</strong>.</p>";
echo "<p>Classification: <strong>{$category}</strong>.</p>";
echo "</div>";
echo "<h3>Weight Matrix for Your Height ({$height_in} inches)</h3>";
echo "<p><small>* The row displays your exact configuration.</small></p>";
echo "<table>";
echo "<tr><th>Weight (lbs)</th><th>Calculated BMI</th><th>Health Status</th></tr>";
// Dynamic boundaries surrounding your specific weight bracket
$start_weight = max(80, round($weight_lbs) - 20);
$end_weight = round($weight_lbs) + 20;
$inserted_exact_row = false;
for ($w = $start_weight; $w <= $end_weight; $w += 5) {
// Inject the exact calculated user data row chronologically into the list
if (!$inserted_exact_row && $weight_lbs < $w) {
echo "<tr class='current-row'>";
echo "<td>{$weight_lbs} lbs (Your Weight)</td>";
echo "<td>{$bmi}</td>";
echo "<td>{$category}</td>";
echo "</tr>";
$inserted_exact_row = true;
}
$loop_bmi = round(($w / ($height_in * $height_in)) * 703, 1);
if ($loop_bmi < 18.5) {
$loop_status = "Underweight";
} else if ($loop_bmi >= 18.5 && $loop_bmi < 25.0) {
$loop_status = "Normal";
} else if ($loop_bmi >= 25.0 && $loop_bmi < 30.0) {
$loop_status = "Overweight";
} else {
$loop_status = "Obese";
}
// Fallback match check for clean intervals
if (abs($w - $weight_lbs) < 0.01) {
echo "<tr class='current-row'>";
echo "<td>{$w} lbs (Your Weight)</td>";
echo "<td>{$loop_bmi}</td>";
echo "<td>{$loop_status}</td>";
echo "</tr>";
$inserted_exact_row = true;
continue;
}
echo "<tr>";
echo "<td>{$w} lbs</td>";
echo "<td>{$loop_bmi}</td>";
echo "<td>{$loop_status}</td>";
echo "</tr>";
}
// Catch edge placement if exact weight is above the loop range endpoint
if (!$inserted_exact_row) {
echo "<tr class='current-row'>";
echo "<td>{$weight_lbs} lbs (Your Weight)</td>";
echo "<td>{$bmi}</td>";
echo "<td>{$category}</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p class='error'>Error: Please enter positive, non-zero values for height and weight.</p>";
}
}
?>
<?php include('includes/footer.php'); ?>