<?php
include('../includes/header.php');
error_reporting(E_ALL);
echo('<h1>Jess\'s Sleep Tracker</h1>');
// Note the variables being passed via the POST ternary operators.
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$sleep = isset($_POST['sleep']) ? $_POST['sleep'] : array();
// filter all input taken from the Browser
$step = htmlentities($step);
// If sleep is an array, scrub each value
if (is_array($sleep))
{
foreach ($sleep as $key => $value)
{
$sleep[$key] = htmlentities($value);
}
}
if ($step == 0) // first time into this form
{
?>
<form action="sleep_tracker.php" method="POST">
<fieldset>
<legend>Sleep Tracker (7 Nights)</legend>
<p>Enter how many hours you slept each night (you can use decimals like 5.5).</p>
<label for="night1">Night 1</label>
<input type="text" size="10" id="night1" name="sleep[]" value="">
<br><br>
<label for="night2">Night 2</label>
<input type="text" size="10" id="night2" name="sleep[]" value="">
<br><br>
<label for="night3">Night 3</label>
<input type="text" size="10" id="night3" name="sleep[]" value="">
<br><br>
<label for="night4">Night 4</label>
<input type="text" size="10" id="night4" name="sleep[]" value="">
<br><br>
<label for="night5">Night 5</label>
<input type="text" size="10" id="night5" name="sleep[]" value="">
<br><br>
<label for="night6">Night 6</label>
<input type="text" size="10" id="night6" name="sleep[]" value="">
<br><br>
<label for="night7">Night 7</label>
<input type="text" size="10" id="night7" name="sleep[]" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Analyze Sleep">
</fieldset>
</form>
<?php
}
else // else show what the form gathered and analyze
{
// --- Analyze Sleep (decisions + loops) ---
$total_sleep = 0;
$nights_counted = 0;
$under_six = 0;
$healthy = 0; // 6 to 8 hours
$over_eight = 0;
$min_sleep = null;
$max_sleep = null;
foreach ($sleep as $hours)
{
// Convert to float for math
$hours_num = (float)$hours;
// Ignore blank entries (optional)
if ($hours === '')
{
continue;
}
$total_sleep += $hours_num;
$nights_counted++;
// track min and max
if ($min_sleep === null || $hours_num < $min_sleep)
{
$min_sleep = $hours_num;
}
if ($max_sleep === null || $hours_num > $max_sleep)
{
$max_sleep = $hours_num;
}
// decisions for categories
if ($hours_num < 6)
{
$under_six++;
}
elseif ($hours_num <= 8)
{
$healthy++;
}
else
{
$over_eight++;
}
}
if ($nights_counted == 0)
{
echo('<h3>Please enter at least one night of sleep hours.</h3>');
}
else
{
$avg_sleep = $total_sleep / $nights_counted;
echo('<h2>Results</h2>');
echo('<h3>Average Sleep: ' . number_format($avg_sleep, 2) . ' hours</h3>');
echo('<p>Under 6 hours: ' . $under_six . ' night(s)</p>');
echo('<p>6–8 hours: ' . $healthy . ' night(s)</p>');
echo('<p>Over 8 hours: ' . $over_eight . ' night(s)</p>');
echo('<p>Lowest night: ' . number_format($min_sleep, 2) . ' hours</p>');
echo('<p>Highest night: ' . number_format($max_sleep, 2) . ' hours</p>');
// overall evaluation
if ($avg_sleep < 6)
{
echo('<h3>Overall: Sleep deprived 😴</h3>');
}
elseif ($avg_sleep <= 8)
{
echo('<h3>Overall: Healthy sleep range 👍</h3>');
}
else
{
echo('<h3>Overall: Oversleeping? 😴</h3>');
}
}
// provide a form to try again
?>
<form action="sleep_tracker.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('../includes/footer.php');
?>