298 lines
8.8 KiB
PHP
298 lines
8.8 KiB
PHP
<?php
|
|
function calc_bmr($gender, $age, $weight, $height)
|
|
{
|
|
if ($gender === 'female') {
|
|
return 655.1 + (9.6 * $weight) + (1.8 * $height) - (4.7 * $age);
|
|
}
|
|
return 66.47 + (13.7 * $weight) + (5 * $height) - (6.8 * $age);
|
|
}
|
|
|
|
$errors = [];
|
|
$result = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$gender = isset($_POST['gender']) ? $_POST['gender'] : '';
|
|
$age = floatval(isset($_POST['age']) ? $_POST['age'] : 0);
|
|
$weight = floatval(isset($_POST['weight']) ? $_POST['weight'] : 0);
|
|
$height = floatval(isset($_POST['height']) ? $_POST['height'] : 0);
|
|
|
|
$hours = [
|
|
'sitting' => floatval(isset($_POST['sitting']) ? $_POST['sitting'] : 0),
|
|
'office' => floatval(isset($_POST['office']) ? $_POST['office'] : 0),
|
|
'standing' => floatval(isset($_POST['standing']) ? $_POST['standing'] : 0),
|
|
'light_ex' => floatval(isset($_POST['light_ex']) ? $_POST['light_ex'] : 0),
|
|
'moderate_ex' => floatval(isset($_POST['moderate_ex']) ? $_POST['moderate_ex'] : 0),
|
|
'vigorous_ex' => floatval(isset($_POST['vigorous_ex']) ? $_POST['vigorous_ex'] : 0),
|
|
];
|
|
|
|
if (!in_array($gender, ['male', 'female'])) {
|
|
$errors[] = 'Please select gender.';
|
|
}
|
|
if ($age <= 0) {
|
|
$errors[] = 'Please enter a valid age.';
|
|
}
|
|
if ($weight <= 0) {
|
|
$errors[] = 'Please enter a valid weight.';
|
|
}
|
|
if ($height <= 0) {
|
|
$errors[] = 'Please enter a valid height.';
|
|
}
|
|
|
|
$sumHours = array_sum($hours);
|
|
if ($sumHours > 24) {
|
|
$errors[] = 'Sum of entered hours exceeds 24.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$pal = [
|
|
'sitting' => 1.2,
|
|
'office' => 1.45,
|
|
'standing' => 1.85,
|
|
'light_ex' => 2.0,
|
|
'moderate_ex' => 3.0,
|
|
'vigorous_ex' => 4.0,
|
|
'sleep' => 0.95
|
|
];
|
|
|
|
$sleepHours = max(0, 24 - $sumHours);
|
|
$weighted = 0.0;
|
|
foreach ($hours as $k => $h) {
|
|
$weighted += $h * $pal[$k];
|
|
}
|
|
$weighted += $sleepHours * $pal['sleep'];
|
|
$avgPal = $weighted / 24.0;
|
|
|
|
$bmr = calc_bmr($gender, $age, $weight, $height);
|
|
$dailyCalories = $bmr * $avgPal;
|
|
|
|
$result = [
|
|
'bmr' => $bmr,
|
|
'avgPal' => $avgPal,
|
|
'dailyCalories' => $dailyCalories,
|
|
'dailyCalories_loss_400' => max(0, $dailyCalories - 400),
|
|
'sleepHours' => $sleepHours
|
|
];
|
|
}
|
|
}
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Kalorienrechner</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
background-color: #000000;
|
|
color: #ffffff;
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
line-height: 1.6;
|
|
padding: 20px;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 2.5em;
|
|
margin-bottom: 30px;
|
|
text-align: center;
|
|
border-bottom: 2px solid #ffffff;
|
|
padding-bottom: 15px;
|
|
}
|
|
|
|
h2 {
|
|
font-size: 2em;
|
|
margin-top: 30px;
|
|
margin-bottom: 20px;
|
|
border-bottom: 1px solid #ffffff;
|
|
padding-bottom: 10px;
|
|
}
|
|
|
|
h3 {
|
|
font-size: 1.3em;
|
|
margin-top: 25px;
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.error-container {
|
|
background-color: #330000;
|
|
border: 2px solid #ff0000;
|
|
border-radius: 5px;
|
|
padding: 15px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.error-container ul {
|
|
list-style-position: inside;
|
|
color: #ff6666;
|
|
}
|
|
|
|
.error-container li {
|
|
margin: 5px 0;
|
|
}
|
|
|
|
form {
|
|
background-color: #1a1a1a;
|
|
padding: 30px;
|
|
border-radius: 10px;
|
|
border: 1px solid #333333;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-bottom: 15px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
input[type="number"] {
|
|
background-color: #000000;
|
|
color: #ffffff;
|
|
border: 1px solid #ffffff;
|
|
padding: 8px 12px;
|
|
border-radius: 4px;
|
|
font-size: 1em;
|
|
width: 200px;
|
|
margin-left: 10px;
|
|
}
|
|
|
|
input[type="number"]:focus {
|
|
outline: none;
|
|
border-color: #ffffff;
|
|
box-shadow: 0 0 5px #ffffff;
|
|
}
|
|
|
|
input[type="radio"] {
|
|
margin-left: 15px;
|
|
margin-right: 5px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button {
|
|
background-color: #ffffff;
|
|
color: #000000;
|
|
border: none;
|
|
padding: 12px 30px;
|
|
font-size: 1.1em;
|
|
font-weight: bold;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
margin-top: 20px;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
button:hover {
|
|
background-color: #cccccc;
|
|
transform: scale(1.05);
|
|
}
|
|
|
|
button:active {
|
|
transform: scale(0.98);
|
|
}
|
|
|
|
.results-container {
|
|
background-color: #1a1a1a;
|
|
padding: 30px;
|
|
border-radius: 10px;
|
|
margin-top: 30px;
|
|
border: 1px solid #333333;
|
|
}
|
|
|
|
.results-container p {
|
|
margin: 10px 0;
|
|
font-size: 1.1em;
|
|
}
|
|
|
|
.results-container b {
|
|
color: #ffffff;
|
|
font-size: 1.2em;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Kalorienrechner</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="error-container">
|
|
<ul>
|
|
<?php foreach ($errors as $e): ?>
|
|
<li><?php echo htmlspecialchars($e); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post">
|
|
<label>
|
|
Gender:
|
|
<input type="radio" name="gender" value="male" <?php if ((isset($gender) ? $gender : '') === 'male') echo 'checked'; ?>> Male
|
|
<input type="radio" name="gender" value="female" <?php if ((isset($gender) ? $gender : '') === 'female') echo 'checked'; ?>> Female
|
|
</label>
|
|
|
|
<label>
|
|
Age:
|
|
<input type="number" name="age" step="1" value="<?php echo htmlspecialchars(isset($_POST['age']) ? $_POST['age'] : ''); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Weight (kg):
|
|
<input type="number" name="weight" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['weight']) ? $_POST['weight'] : ''); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Height (cm):
|
|
<input type="number" name="height" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['height']) ? $_POST['height'] : ''); ?>">
|
|
</label>
|
|
|
|
<h3>Enter hours spent per activity (total can be <= 24; remaining = sleep)</h3>
|
|
|
|
<label>
|
|
Sitting (PAL 1.20):
|
|
<input type="number" name="sitting" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['sitting']) ? $_POST['sitting'] : '0'); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Office (PAL 1.45):
|
|
<input type="number" name="office" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['office']) ? $_POST['office'] : '0'); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Standing/Walking (PAL 1.85):
|
|
<input type="number" name="standing" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['standing']) ? $_POST['standing'] : '0'); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Light exercise (PAL 2.00):
|
|
<input type="number" name="light_ex" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['light_ex']) ? $_POST['light_ex'] : '0'); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Moderate exercise (PAL 3.00):
|
|
<input type="number" name="moderate_ex" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['moderate_ex']) ? $_POST['moderate_ex'] : '0'); ?>">
|
|
</label>
|
|
|
|
<label>
|
|
Vigorous exercise (PAL 4.00):
|
|
<input type="number" name="vigorous_ex" step="0.1" value="<?php echo htmlspecialchars(isset($_POST['vigorous_ex']) ? $_POST['vigorous_ex'] : '0'); ?>">
|
|
</label>
|
|
|
|
<button type="submit">Calculate</button>
|
|
</form>
|
|
|
|
<?php if ($result): ?>
|
|
<div class="results-container">
|
|
<h2>Results</h2>
|
|
<p>BMR: <?php echo number_format($result['bmr'], 2); ?> kcal/day</p>
|
|
<p>Average PAL: <?php echo number_format($result['avgPal'], 2); ?></p>
|
|
<p>Sleep hours (calculated): <?php echo number_format($result['sleepHours'], 1); ?> h</p>
|
|
<p><b>Daily calories to maintain weight:</b> <?php echo number_format($result['dailyCalories'], 0); ?> kcal</p>
|
|
<p>To lose weight: about <?php echo number_format($result['dailyCalories_loss_400'], 0); ?> kcal (≈ -400 kcal)</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</body>
|
|
</html>
|