Veronica's Grade Calculating Form
Enter your grades for each exam and assignment
 
<?php
include('includes/header.php');
// set error reporting to all
error_reporting(E_ALL);
// form heading
echo('<h1>Veronica\'s Grade Calculating Form</h1>');
// variables
$self = basename($_SERVER['SCRIPT_NAME']);
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$exam1 = isset($_POST['exam1']) ? $_POST['exam1'] : null;
$exam2 = isset($_POST['exam2']) ? $_POST['exam2'] : null;
$exam3 = isset($_POST['exam3']) ? $_POST['exam3'] : null;
$exam4 = isset($_POST['exam4']) ? $_POST['exam4'] : null;
$exam5 = isset($_POST['exam5']) ? $_POST['exam5'] : null;
$assgn1 = isset($_POST['assgn1']) ? $_POST['assgn1'] : null;
$assgn2 = isset($_POST['assgn2']) ? $_POST['assgn2'] : null;
$assgn3 = isset($_POST['assgn3']) ? $_POST['assgn3'] : null;
$assgn4 = isset($_POST['assgn4']) ? $_POST['assgn4'] : null;
$assgn5 = isset($_POST['assgn5']) ? $_POST['assgn5'] : null;
//$exams = array();
//$assgns = array();
// filter all input taken from the Browser
$step = htmlentities($step);
// start
if ($step == 0) // first time into this form
{
?>
<h3>Enter your grades for each exam and assignment </h3>
<br>
<form action="<?php echo($self);?>" method="POST" >
<h4>Enter your numeric grades for each exam (percent) </h4>
<br>
<label for="exam1">Exam 1</label>
<input type="text" size="24" id="exam1" name="exam1" value="">
<br><br>
<label for="exam2">Exam 2</label>
<input type="text" size="24" id="exam2" name="exam2" value="">
<br><br>
<label for="exam3">Exam 3</label>
<input type="text" size="24" id="exam3" name="exam3" value="">
<br><br>
<label for="exam4">Exam 4</label>
<input type="text" size="24" id="exam4" name="exam4" value="">
<br><br>
<label for="exam5">Exam 5</label>
<input type="text" size="24" id="exam5" name="exam5" value="">
<br><br>
<h4>Enter your numeric grades for each assignment (percent) </h4>
<br>
<label for="assgn1">Assignment 1</label>
<input type="text" size="24" id="assgn1" name="assgn1" value="">
<br><br>
<label for="assgn2">Assignment 2</label>
<input type="text" size="24" id="assgn2" name="assgn2" value="">
<br><br>
<label for="assgn3">Assignment 3</label>
<input type="text" size="24" id="assgn3" name="assgn3" value="">
<br><br>
<label for="assgn4">Assignment 4</label>
<input type="text" size="24" id="assgn4" name="assgn4" value="">
<br><br>
<label for="assgn5">Assignment 5</label>
<input type="text" size="24" id="assgn5" name="assgn5" value="">
<br><br>
<br><br>
<br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value=" Submit ">
</form>
<?php
}
else
{
// results...
// get input from the Browser
$exam1 = htmlentities($exam1);
$exam2 = htmlentities($exam2);
$exam3 = htmlentities($exam3);
$exam4 = htmlentities($exam4);
$exam5 = htmlentities($exam5);
$assgn1 = htmlentities($assgn1);
$assgn2 = htmlentities($assgn2);
$assgn3 = htmlentities($assgn3);
$assgn4 = htmlentities($assgn4);
$assgn5 = htmlentities($assgn5);
echo("<h3>Results: </h3><br>");
// call functions to create arrays; pass true for exams
$exams = create_array($exam1, $exam2, $exam3, $exam4, $exam5, true);
$assgns = create_array($assgn1, $assgn2, $assgn3, $assgn4, $assgn5, false);
// call functions to calculate averages
$exam_avg = calculate_average($exams);
$assgn_avg = calculate_average($assgns);
$final_avg = calculate_final_average($exams, $assgns);
// call functions to calculate final scores
$exam_score = calculate_score($exams);
$assgn_score = calculate_score($assgns);
$final_score = calculate_final_score($exams, $assgns);
// call function to get grades
$exam_grade = get_grade($exam_score);
$assgn_grade = get_grade($assgn_score);
$grade = get_grade($final_score);
// display
echo('<pre>');
echo("Your exam average: $exam_avg ");
echo('<br>');
echo("Your assignment average: $assgn_avg ");
echo('<br>');
echo("Your exams grade: $exam_grade ");
echo('<br>');
echo("Your assignments grade: $assgn_grade ");
echo('<br>');
echo('</pre>');
echo('<br>');
echo('<pre>');
echo('<H3>');
echo("Your Final Score = $final_score <br>");
echo("Your Final Grade = $grade <br>");
echo('</H3>');
if ($grade == 'A')
{
echo('<H3>');
echo("CONGRATULATIONS! ");
echo('</H3>');
}
echo('</pre>');
// provide a fresh form to try again
?>
<form action="<?php echo($self);?>" method="POST" >
<br>
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value=" Try Again ">
</form>
<?php
}
// functions
function create_array($score1, $score2, $score3, $score4, $score5, $tf)
{
// populate array
$score_array[0] = $score1;
$score_array[1] = $score2;
$score_array[2] = $score3;
$score_array[3] = $score4;
$score_array[4] = $score5;
// remove null scores
for ($i = 0; $i < count($score_array); $i++)
{
if ($score_array[$i] == null) {
unset($score_array[$i]);
}
}
if ($score_array[4] == null)
{
unset($score_array[$i]);
}
// entries count
$c = count($score_array);
echo('<pre>');
echo("Your <br>");
if ($tf)
{
echo("Exam <br>");
} else
{
echo("Assignment <br>");
}
echo("Score <br>");
print_r($score_array);
echo("<br> for $c entries");
echo('</pre>');
return $score_array;
}
function calculate_average($assgns)
{
// average of subset of scores
$c = count($assgns);
$avg = array_sum($assgns) / $c;
return $avg;
}
function calculate_final_average($exams, $assgns)
{
// average of all scores
$c = count($exams) + count($assgns);
$s = array_sum($exams) + array_sum($assgns);
$final_avg = $s / $c;
return $final_avg;
}
function calculate_score($scores)
{
// calculate with subset of scores
$c = count($scores);
$possible = 100 * $c;
$score = (array_sum($scores) / $possible) * 100;
return $score;
}
function calculate_final_score($exams, $assgns)
{
// calculate with all scores
$c = count($exams) + count($assgns);
$possible = 100 * $c;
$final_sum = (array_sum($exams)) + (array_sum($assgns));
$final_score = ($final_sum / $possible) * 100;
return $final_score;
}
function get_grade($score)
{
switch ($score)
{
case ($score > 90):
$grade = 'A';
break;
case ($score > 80 && $score <= 90):
$grade = 'B';
break;
case ($score > 70 && $score <= 80):
$grade = 'C';
break;
case ($score > 60 && $score <= 70):
$grade = 'D';
break;
default:
$grade = 'E';
break;
}
return $grade;
}
// footer
include('includes/footer.php');
?>