Daily Calorie Needs Calculator
CODE FOLLOWS
<?php
include('includes/header.php');
error_reporting(E_ALL);
echo('<h1>Daily Calorie Needs Calculator</h1>');
// variables passed via POST
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$weight = isset($_POST['weight']) ? $_POST['weight'] : '';
$height = isset($_POST['height']) ? $_POST['height'] : '';
$age = isset($_POST['age']) ? $_POST['age'] : '';
$activity = isset($_POST['activity']) ? $_POST['activity'] : '';
// sanitize input
$step = htmlentities($step);
$weight = htmlentities($weight);
$height = htmlentities($height);
$age = htmlentities($age);
$activity = htmlentities($activity);
if ($step == 0) {
// display form
?>
<form method="POST" class="pad">
<label for="weight">Weight (lbs):</label>
<input type="text" id="weight" name="weight" value="">
<br><br>
<label for="height">Height (inches):</label>
<input type="text" id="height" name="height" value="">
<br><br>
<label for="age">Age (years):</label>
<input type="text" id="age" name="age" value="">
<br><br>
<label for="activity">Activity Level:</label>
<select id="activity" name="activity">
<option value="1.2">Sedentary</option>
<option value="1.375">Lightly active</option>
<option value="1.55">Moderately active</option>
<option value="1.725">Very active</option>
<option value="1.9">Extra active</option>
</select>
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Calculate Calories">
</form>
<?php
} else {
// show results
echo('<p>The following is what the form gathered:</p>');
echo('<pre>');
print_r($_POST);
echo('</pre>');
if ($weight > 0 && $height > 0 && $age > 0 && $activity > 0) {
// Basic Mifflin-St Jeor Equation for Men (you can also add a gender field)
$bmr = (10 * ($weight * 0.453592)) + (6.25 * ($height * 2.54)) - (5 * $age) + 5; // weight in kg, height in cm
$calories = round($bmr * $activity);
echo("<h3>Estimated Daily Calories: $calories kcal</h3>");
} else {
echo('<p>Invalid input provided.</p>');
}
// try again form
?>
<form method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>