Jump to content

[SOLVED] Average of 3 numeric form inputs, with output of a GRADE average


suttercain

Recommended Posts

Hello,

 

I am trying to have the user enter 3 numeric values in a form. After it is submitted, I want the out put to show their grade average.

 

Here is my code

 

HTML:

<body>
<form action="lab_1b.php" method="post"> 
What was your first score?<br>
<input type="text" name="grade1" size=3 /><p></p>

What was your second score?<br>
<input type="text" name="grade2" size=3 /><p></p>

What was your third score?<br>
<input type="text" name="grade3" size=3 /><p></p>
<input type="submit" name="grades" value="Submit" />
</form>

</body>

 

PHP side

<?php
extract ($_REQUEST);

if ($grade1 + $grade2 + $grade3 / 3  >= 90) {
print "You got an A!";
}

elseif ($grade1 + $grade2 + $grade3 / 3   >= 80 or <=89) {
print "You got a B!";
}

elseif ($grade1 + $grade2 + $grade3 / 3   >= 70 or <=79) {
print "You got a C!";
}

elseif ($grade1 + $grade2 + $grade3 / 3   >= 60 or <=69) {
print "You got a D!";
}

else {
print "You got a F!";
}
?>

 

I am getting this error:

 

Parse error: parse error, unexpected T_IS_SMALLER_OR_EQUAL in D:\wamp\www\PHP Tutorials\By Example\examples\chapter7_examples\lab_1b.php on line 16

 

Link to comment
Share on other sites

Should be:

<?php
extract ($_REQUEST);

$total = $grade1 + $grade2 + $grade3 / 3;

if ($total >= 90) {
print "You got an A!";
}

elseif ($total  >= 80 || $total <=89) {
print "You got a B!";
}

elseif ($total  >= 70 || $total <=79) {
print "You got a C!";
}

elseif ($total  >= 60 || $$total  <=69) {
print "You got a D!";
}

else {
print "You got a F!";
}
?>

Link to comment
Share on other sites

Try:

<?php
extract ($_REQUEST);

$total = $grade1 + $grade2 + $grade3 / 3;

if ($total >= 90) {
print "You got an A!";
}

elseif ($total  >= 80 && $total <=89) {
print "You got a B!";
}

elseif ($total  >= 70 && $total <=79) {
print "You got a C!";
}

elseif ($total  >= 60 && $$total  <=69) {
print "You got a D!";
}

else {
print "You got a F!";
}
?>

Link to comment
Share on other sites

Tested and working properly.

 

 

<?php
if(!isset($_POST['grades'])){
?>
<form action="<?=$_POST['PHP_SELF'] ?>" method="post"> 
What was your first score?<br>
<input type="text" name="grade1" size=3 /><p></p>

What was your second score?<br>
<input type="text" name="grade2" size=3 /><p></p>

What was your third score?<br>
<input type="text" name="grade3" size=3 /><p></p>
<input type="submit" name="grades" value="Submit" />
</form>

<?
}else{
$grade1=$_POST['grade1'];
$grade2=$_POST['grade2'];
$grade3=$_POST['grade3'];

$total=($grade1 + $grade2 + $grade3) / 3; // set all math here so you only have to do it 1 time

echo 'Your average is '. sprintf("%.2f",$total) .'% <br><br>'; // used sprintf to cut off at 2 decimal places

if ($total  >= 90) {
print "You got an A!";
}

elseif ($total >= 80 && $total <= 89) { // changed the or to &&
print "You got a B!";
}

elseif ($total   >= 70 && $total <=79) { // changed the or to &&
print "You got a C!";
}

elseif ($total   >= 60 && $total <=69) { // changed the or to &&
print "You got a D!";
}

else {
print "You got a F!";
}



}
?>

 

I tested this and it works. You must specify the &&  because you want the condition to return true if it is greater than X AND less than X. With the || (or clause) it was giving an A for a 60%

 

I moved the processing to the same page as the form so I could make it work, if you need to just grab everything in between the else { } and put it in your processing page.

 

Hope this helps.

Link to comment
Share on other sites

what 3 numbers did you use to get that?

 

It is because of the structure of the if else statement.

 

elseif ($total   >= 70 && $total <=79) {
print "You got a C!";
}

 

79.67 is greater than 70 but not less than 79. Try this. I don't remember the grading scales exactly, but if I remember correctly, 79.99% is still in the C range... like a C+

 

<?php
if(!isset($_POST['grades'])){
?>
<form action="<?=$_POST['PHP_SELF'] ?>" method="post"> 
What was your first score?<br>
<input type="text" name="grade1" size=3 /><p></p>

What was your second score?<br>
<input type="text" name="grade2" size=3 /><p></p>

What was your third score?<br>
<input type="text" name="grade3" size=3 /><p></p>
<input type="submit" name="grades" value="Submit" />
</form>

<?
}else{
$grade1=$_POST['grade1'];
$grade2=$_POST['grade2'];
$grade3=$_POST['grade3'];

$total=($grade1 + $grade2 + $grade3) / 3;

echo 'Your average is '. sprintf("%.2f",$total) .'% <br>';

if ($total  >= 90) {
print "You got an A!";
}

elseif ($total >= 80 && $total <= 89.99) {
print "You got a B!";
}

elseif ($total   >= 70 && $total <=79.99) {
print "You got a C!";
}

elseif ($total   >= 60 && $total <=69.99) {
print "You got a D!";
}

elseif($total < 60){
print "You got a F!";
}
else { // this is there just to account for anything that is not covered in the above statements.
print "Score was out of range, need to adjust code to account for this percentage";

}
}
?>

Link to comment
Share on other sites

I changed the numbers to floating points and that fixed it. Another question I have is how do I set it so that the user must enter a number.

 

I know is_numeric() is the right path, I just don't know how to use it.

 

<?php
if(!isset($_POST['grades'])){
}else{
$grade1=$_POST['grade1'];
$grade2=$_POST['grade2'];
$grade3=$_POST['grade3'];

$total=($grade1 + $grade2 + $grade3) / 3; // set all math here so you only have to do it 1 time

echo 'Your average is '. sprintf("%.2f",$total) .'% <br>'; // used sprintf to cut off at 2 decimal places
if ($total  >= 90) {
print "<h1>You got an A!</h1>";
}
elseif ($total >= 80 && $total <= 89.9999) { // changed the or to &&
print "You got a B!";
}
elseif ($total >= 70 && $total <=79.9999) { // changed the or to &&
print "You got a C!";
}
elseif ($total   >= 60 && $total <=69.9999) { // changed the or to &&
print "You got a D!";
}
else {
print "You got a F!";
}
}
?>

 

I want to make it so the user has to enter a number, and cannot pass a character.

Link to comment
Share on other sites

I placed the numeric check part right in the top as you want to ensure all values are indeed numeric before processing. So the statement reads, if all 3 items are numeric, then process (scroll to bottom of code now.)

 

 

<?php
if(!isset($_POST['grades'])){
}else{

if(is_numeric($_POST['grade1']) && is_numeric($_POST['grade2']) && is_numeric($_POST['grade3']) ){


$grade1=$_POST['grade1'];
$grade2=$_POST['grade2'];
$grade3=$_POST['grade3'];

$total=($grade1 + $grade2 + $grade3) / 3; // set all math here so you only have to do it 1 time

echo 'Your average is '. sprintf("%.2f",$total) .'% <br>'; // used sprintf to cut off at 2 decimal places
if ($total  >= 90) {
print "<h1>You got an A!</h1>";
}
elseif ($total >= 80 && $total <= 89.9999) { // changed the or to &&
print "You got a B!";
}
elseif ($total >= 70 && $total <=79.9999) { // changed the or to &&
print "You got a C!";
}
elseif ($total   >= 60 && $total <=69.9999) { // changed the or to &&
print "You got a D!";
}
else {
print "You got a F!";
}


}else{
echo " All Entries Must Be Numeric";
}
}
?>

 

else at least one entry was not numeric, so print error message.

 

Nate

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.