suttercain Posted March 10, 2007 Share Posted March 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
papaface Posted March 10, 2007 Share Posted March 10, 2007 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!"; } ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted March 10, 2007 Share Posted March 10, 2007 Although Im pretty sure you should be using AND (&&) in those instead of OR (||) Also if you are using register_globals you shouldnt. Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 10, 2007 Author Share Posted March 10, 2007 Hi Papaface, I tried your code and I see what you're doing. But now, no matter which numbers I enter I get a "Uou got a B!" Quote Link to comment Share on other sites More sharing options...
papaface Posted March 10, 2007 Share Posted March 10, 2007 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!"; } ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 10, 2007 Author Share Posted March 10, 2007 Yeah, the "math" is all messed up. If I enter 100, 60, 60 it outputs A when it should be C (73.333) Quote Link to comment Share on other sites More sharing options...
chronister Posted March 10, 2007 Share Posted March 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 10, 2007 Author Share Posted March 10, 2007 Chronic, you're the man. You got me up and going. Thanks. Shannon Quote Link to comment Share on other sites More sharing options...
chronister Posted March 10, 2007 Share Posted March 10, 2007 hehe... chronic My old highschool nickname has come back to haunt me Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 10, 2007 Author Share Posted March 10, 2007 I figured as much Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 10, 2007 Author Share Posted March 10, 2007 Whoa... check this out: Your average is 79.67% You got a F! but Your average is 79.00% You got a C! Quote Link to comment Share on other sites More sharing options...
chronister Posted March 11, 2007 Share Posted March 11, 2007 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"; } } ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 11, 2007 Author Share Posted March 11, 2007 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. Quote Link to comment Share on other sites More sharing options...
chronister Posted March 11, 2007 Share Posted March 11, 2007 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 Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 11, 2007 Author Share Posted March 11, 2007 Thank you sir. That worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.