1)
the program would start off by displaying a page asking the user to input his marks. There will be three text boxes in a form where the user can type in his marks.
The form posts the values to the next page reached by the submit button.
On the next page the php script is called into effect. it takes the values placed in the textboxes and first makes sure that the valua is valid.
The values, if valid, are then added up, and then divide by 3 to get the numerical average.
the code would then translate the average into a letter grade and display it on the page.
2)
code for form page - "page1.htm" (where user types in marks):
<html>
<head>
<title>Your Marks are?</title>
</head>
<body>
<p>Insert your marks below:</p>
<form action="page2.php" method="post" name="marks" id="marks">
<p>Mark 1:
<input name="mark1" type="text" id="mark1">
<br>
Mark 2:
<input name="mark2" type="text" id="mark2">
<br>
Mark 3:
<input name="mark3" type="text" id="mark3">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
Code for generating average and displaying it - page2.php:
<body>
<?php
//place posted data into variables
//this isn't necessary but just for clarification purposes
$mark1 = $_POST["mark1"];
$mark2 = $_POST["mark2"];
$mark3 = $_POST["mark3"];
/*to validate the data by checking that its within the 100 mark range (no less than 0, no higher than 100) and not empty
we use a foreach loop. This foreach loop goes through every textbox ($_POST value) and makes sure it conforms to the rules.
in other words, if a value is missing, a textbox empty, or if the value is not a logical mark, the script will tell you.
by using the echo statement, we keep the script active, so it will tell you which field(s) is missing or wrong. Normally, you would use
the 'die' to stop the script, as we don't want it to calculate wrong and empty values, but for the sake of argument we'll keep it
as such. The <br> tag is there so each error shows up on a different line.*/
foreach($_POST as $key => $value) {
if(!$value) {
echo "$key is missing. Please go back and try again. <br>";
}
else if ($value < 0 OR $value > 100) {
echo "$key is not a valid mark. Please go back and try again. <br>";
}
}
/*once we know that the values are good (no errors) we can then set about calculating the data.
to do this we use a straightforward mathematical equation */
$num_grade = ($mark1 + $mark2 + $mark3) / 3;
//Print out the numerical grade
echo "You're numerical Grade Point Average is: $num_grade <br>";
// With the numerical grade in a variable, we can use if else statments to figure out the letter grade
if ($num_grade > 90) { echo " You got an A "; }
elseif ($num_grade > 80) { echo " You got a B "; }
elseif ($num_grade > 70) { echo " You got a C "; }
elseif ($num_grade > 60) { echo " You got a D "; }
elseif ($num_grade > 50) { echo " You got a E "; }
elseif ($num_grade > 40) { echo " You got a F "; }
else { echo " You are ungraded "; }
?>
I highly recommend the following site to learn PHP quickly for your son:
http://www.daholygoat.com/phptutorial.html
Take care and Good luck.