<?php
include('../includes/header.php');
error_reporting(E_ALL);
echo("<h1>Study Time Calculator</h1>");
// get values from POST
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$mon = isset($_POST['mon']) ? $_POST['mon'] : '';
$tue = isset($_POST['tue']) ? $_POST['tue'] : '';
$wed = isset($_POST['wed']) ? $_POST['wed'] : '';
$thu = isset($_POST['thu']) ? $_POST['thu'] : '';
$fri = isset($_POST['fri']) ? $_POST['fri'] : '';
// basic input filtering
$step = htmlentities($step);
$mon = htmlentities($mon);
$tue = htmlentities($tue);
$wed = htmlentities($wed);
$thu = htmlentities($thu);
$fri = htmlentities($fri);
// FUNCTION 1: total hours
function calculate_total_hours($hours)
{
return array_sum($hours);
}
// FUNCTION 2: average hours
function calculate_average_hours($hours)
{
return array_sum($hours) / count($hours);
}
// FUNCTION 3: highest day
function find_highest_day($hours)
{
return max($hours);
}
// FUNCTION 4: study message
function study_message($average)
{
if ($average < 1)
{
return "You may want to study some more!";
}
elseif ($average <= 3)
{
return "Good study progress!";
}
else
{
return "Great job staying focused!";
}
}
if ($step == 0)
{
?>
<form action="study_time_calculator.php" method="POST">
<p>Enter the number of hours studied for each day.</p>
<label for="mon">Monday: </label>
<input type="text" size="20" id="mon" name="mon" value="">
<br><br>
<label for="tue">Tuesday: </label>
<input type="text" size="20" id="tue" name="tue" value="">
<br><br>
<label for="wed">Wednesday: </label>
<input type="text" size="20" id="wed" name="wed" value="">
<br><br>
<label for="thu">Thursday: </label>
<input type="text" size="20" id="thu" name="thu" value="">
<br><br>
<label for="fri">Friday: </label>
<input type="text" size="20" id="fri" name="fri" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Calculate">
</form>
<?php
}
else
{
$hours = array((float)$mon, (float)$tue, (float)$wed, (float)$thu, (float)$fri);
$total = calculate_total_hours($hours);
$average = calculate_average_hours($hours);
$highest = find_highest_day($hours);
$message = study_message($average);
echo("<h2><strong>Results</strong></h2>");
echo("<p><strong>Total Study Time:</strong> " . number_format($total, 1) . " hours</p>" );
echo("<p><strong>Average Per Day:</strong> " . number_format($average, 1) . " hours</p>" );
echo("<p><strong>Highest Study Day:</strong> " . number_format($highest, 1) . " hours</p>" );
echo("<p><strong>Message:</strong> $message</p>");
?>
<form action="study_time_calculator.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include ('../includes/footer.php');
?>