0
 I need to write a script that allows a user to enter a numeric grade and then determine the letter grade.  Use a PHP document and a HTML document containing a form with 5 text boxes to enter the numeric grades.  Print a message to the user stating whether or not the grade is  valid, the numeric grade can only be be-tween 0 100), and the letter grade for each numeric grade entered. Check what happens if a grade of 0 is entered. The grading scale is listed below. Example if a grade between 90-93 is entered, a letter of A- will display to the screen. If only 4 grades were entered, then only 4 letter grades should be listed to the screen. I also need to determine the GPA based on the grades, Please Help! God Bless re

<?php
$gradeA = $_POST ['gradeA'];
$gradeB = $_POST ['gradeB'];
$gradeC = $_POST ['gradeC'];
$gradeD = $_POST ['gradeD'];
$gradeF = $_POST ['gradeF'];

while($row == ($show_result))

    {
  $grad=$row['GPA']; 
       if ($grad < 65)
             {
                 $grad = 'F';
             }
            else if ($grad<= 66 && $grad >=65)
            {
                $grad = 'D';
            }
             else if ($grad <= 69 && $grad >=67)
            {
                $grad = 'D+'; 
            }
             else if ($grad <= 73 && $grad >=70)
           {
               $grad = 'C-';
           }
            else if ($grad <= 76 && $grad >=74)
          {
             $grad = 'C';
          }
            else if ($grad<= 79 && $grad >=77 )
            {
                $grad = 'C+';
            }
             else if ($grad <= 83 && $grad >=80)
            {
                $grad = 'B-'; 
            }
             else if ($grad <= 86 && $grad >=84)
           {
               $grad = 'B';
           }
              else if ($grad <= 89 && $grad >=87)
           {
               $grad = 'B+';
           }
             else if ($grad <= 93 && $grad >=90)
           {
               $grad = 'A-';
           }
             else if ($grad <= 96 && $grad >=94)
           {
               $grad = 'A';
           }
            else if ($grad >= 97)
          {
             $grad = 'A+';
          }

           echo "<td>" . $grad . "</td>";
    }
?>    

HTML
<form id="form1" name="form1" method="post">
  <h2>Student Grade Report</h2>
  <p>
    <label for="gradeA">Grade A:</label>
    <input type="text" name="gradeA" id="gradeA">
  Grade is valid</p>
  <p>
    <label for="gradeB">Grade B:</label>
    <input type="text" name="gradeB" id="gradeB">
  Grade is valid</p>
  <p>
    <label for="gradeC">Grade C:</label>
    <input type="text" name="gradeC" id="gradeC">
  Grade is valid</p>
  <p>
    <label for="gradeD">Grade D:</label>
    <input type="text" name="gradeD" id="gradeD">
  Grade is valid</p>
  <p>
    <label for="gradeF">Grade F:</label>
    <input type="text" name="gradeF" id="gradeF">
  Grade is valid</p>
  <p>
    <input name="submit" type="submit" id="submit" formaction="test7.php" formmethod="POST" onClick="MM_validateForm('gradeA','','NinRange90:100','gradeB','','NinRange80:89','gradeC','','NinRange70:79','gradeD','','NinRange65:69','gradeF','','NinRange0:64');return document.MM_returnValue" value="Submit">
  </p>
</form>

   Gradeing Scale
    97 – 100%   A+
    94 – 96%    A
    90 – 93%    A-
    87 – 89%    B+
    84 – 86%    B
    80 – 83%    B-
    77 – 79%    C+
    74 – 76%    C
    70 – 73%    C-
    67 – 69%    D+
    65 – 66%    D
    < 65%        F
3
Contributors
13
Replies
38
Views
3 Years
Discussion Span
Last Post by burt.munn
0

This is not very clear. Your code doesn't make much sense either.

DO you want the user to type in grade boundariers for each grade and THEN type in their own marks?

0

I'm very new to this language. I need to be able to enter a numeric grade and determine the letter grade as well as well as figurring out the GPA. This has to be with HTML and PHP. I can't even get anything to display other than an F across the screen. Sorry so confusing, I guess the code sucks! Thanks

0

OK, still not entirely sure e=what you need. I've knocked up some quick code as a DEMO:

<?php
    $num_boxes = 5;
    $precision = 1;
    $gradetext = '';
                    //minimum=>grade
    $grade_array = array(
                    '97'=>'A+',
                    '94'=>'A',
                    '90'=>'A-',
                    '87'=>'B+',
                    '84'=>'B',
                    '80'=>'B-',
                    '77'=>'C+',
                    '74'=>'C',
                    '70'=>'C-',
                    '67'=>'D+',
                    '65'=>'D',
                    '0'=>'F'
                    );



function create_form($num_boxes=1)
{
    if(is_int($num_boxes))
    {
        $form = '';
        for($i=0;$i<$num_boxes;$i++)
        {
            $inc = $i+1;
            $val = (isset($_POST['grade'][$i])) ? $_POST['grade'][$i] : '';
            $form .= "<label for='grade$inc'>Grade $inc:</label><input id='grade$inc' name='grade[$i]' type='number' value = '$val' min='0' max='100' />"; 
        }
        return $form;
    }
}

function get_grade($pc,$grade_array)
{
    foreach($grade_array as $min=>$grade)
    {
        if($pc < $min) continue;
        return $grade;
    }
}

if($_POST)
{
    $GPA = 0;
    $num_posts = count(array_filter($_POST['grade'], "is_numeric"));
    $total = array_sum($_POST['grade']);
    if($num_posts != 0) $GPA = $total/$num_posts;
    $lettergrade = get_grade($GPA,$grade_array);
    $gradetext = "The GPA = " . number_format($GPA,$precision) . " ($lettergrade)";
}

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php echo $gradetext;?>
<form method="post">
    <?php echo create_form($num_boxes);?>
    <input type="submit" value="Calculate" />
</form>
</body>
</html>

The first two variables are probably the ones you'll need to change to suit yourself - the number of grades to display and the precision of the GPA calculation.

0
    That didn't work for me, so I've been trying to come up with another script. This ones not working correctly either. In my HTML document I have 5 text boxes labled grade 1, grade 2, grade 3, grade 4 and grade 5. I also heave a text box that is labled "Your GPA is" and a submit button. I thought that I could use the switch function but, that doesn't work either. In my php document, I tried to put all the grades ie. $grade1, $grade2, $grade3, $grade4 and $grade5 in the case of the grading criteria and that didn't work. I also tried to get the GPA working and no such luck. Can I use the switch function to do this or do I have the incorrect function.? I can only use PHP and HTML, I can't use Java Script or anything else. This is what I need to be able to do when i'm done. I pull up the HTML page that has my text boxes and submit button. I put a numeric grade in the text boxes and when I hit submit, a letter grade will appear for each numeric grade that was put in the text box that says "Your grade is, with the letter grade displaying."also text will appear that says "Your GPA is" and have the numeric GPA displayed. Can anyone please help me out?
    PHP Document:       



    <?php

    $grade1 = $_POST["grade1"];
    $grade2 = $_POST["grade2"];
    $grade3 = $_POST["grade3"];
    $grade4 = $_POST["grade4"];
    $grade5 = $_POST["grade5"];

    $A = 4.0;
    $B = 3.0;
    $C = 2.0;
    $D = 1.0;


    if (!empty($grade1)) {
        switch (true) {
           case $grade1>=97:
                 echo "<p>Your Grade is A+</p>";
                 break;
           case $grade1>=94 && $grade1<=96:
                 echo "<p>Your Grade is A</p>";
                 break;
           case $grade1>=90 && $grade1<=93:
                 echo "<p>Your Grade is A-</p>";
                 break;
           case $grade1>=87 && $grade1<=89:
                 echo "<p>Your Grade is B+</p>";
                 break;
           case $grade1>=84 && $grade1<=86:
                 echo "<p>Your Grade is B</p>";
                 break;
           case $grade1>=80 && $grade1<=83:
                 echo "<p>Your Grade is B-</p>";
                 break;
           case $grade1>=77 && $grade1<=79:
                 echo "<p>Your Grade is C+</p>";
                 break;
           case $grade1>=74 && $grade1<=76:
                 echo "<p>Your Grade is C</p>";
                 break;
           case $grade1>=70 && $grade1<=73:
                 echo "<p>Your Grade is C-</p>";
                 break;
           case $grade1>=67 && $grade1<=69:
                 echo "<p>Your Grade is D+</p>";
                 break;
           case $grade1>=65 && $grade1<=66:
                 echo "<p>Your Grade is D</p>";
                 break;
           case $grade1<65:
                 echo "<p>Your Grade is F</p>";
                 break;
           case $grade1=0:
                 echo "<p>Incomplete</p>";
                 break;

    $GPA = $A + $B + $C + $D /4;
                echo "<p>Your GPA is</p>";


        }
    }

    ?>


HTML Document




    <form name="table" id="table" action="test10table.php" method="post">
      <h2>Final Grade Calculator</h2>
      <p>
        <label for="grade1">Grade 1:</label>
        <input type="text" name="grade1" id="grade1">
      </p>
      <p>
        <label for="grade2">Grade 2:</label>
        <input type="text" name="grade2" id="grade2">
      </p>
      <p>
        <label for="grade3">Grade 3:</label>
        <input type="text" name="grade3" id="grade3">
      </p>
      <p>
        <label for="grade4">Grade 4:</label>
        <input type="text" name="grade4" id="grade4">
      </p>
      <p>
        <label for="grade5">Grade 5:</label>
        <input type="text" name="grade5" id="grade5">
      </p>
      <p>
        <label for="GPA">Your GPA is:</label>
        <input type="text" name="GPA" id="GPA">
      </p>
      <p>
        <input name="submit" type="submit" id="submit" formaction="test10grade.php" formmethod="POST" onClick="MM_validateForm('grade1','','NinRange0:4','grade2','','RinRange0:100','grade3','','RinRange0:100','grade4','','RinRange0:100','grade5','','RinRange0:100');return document.MM_returnValue" value="Submit">
      </p>
    </form>
0

That didn't work for me, so I've been trying to come up with another script. This ones not working correctly either.

You give up to easily. If you persist with a putative solution, you may get it to work. Strange that every solution that I've provided (and tested) doesn't work for you. Which part of my code didn't work?

0

And I appreciate everything you have helped me with. There are certain things that I need and things I can't have, so when I go in and try to mess with what you gave me, I'm worse off than before. When I put the above code in, the only thing that shows up on the HTML page is the words calculate. Is there more code that I'm missing to put in the html page, am I supposed to keep my HTML the way it was, with the check boxes that I made in there? I told you I was new and I really do put a lot into this, it's very frustrating. I've been sitting here at my desk since 2:30 this morning, almost 12 hours and I'm still struggling. I don't know, maybe I Shouldn't be in school at 57 years old!

0
This is what I'm supposed to have at the top of my PHP page according to my professor. That's for the 5 text boxes in my HTML document

    $grade1 = $_POST["grade1"];
    $grade2 = $_POST["grade2"];
    $grade3 = $_POST["grade3"];
    $grade4 = $_POST["grade4"];
    $grade5 = $_POST["grade5"];

    Can you tell me what the information is below and is something supposed to be the '' in gradetext? Thanks

        $num_boxes = 5;
        $precision = 1;
        $gradetext = '';
0

I told you I was new and I really do put a lot into this, it's very frustrating. I've been sitting here at my desk since 2:30 this morning, almost 12 hours and I'm still struggling. I don't know, maybe I Shouldn't be in school at 57 years old!

Heh, heh. Welcome to the wonderful world of programming! Stick with it, it gets easier - believe me. Had I realised that your were a student, I would have been a lot more cautious in throwing code at you.

...according to my professor...

OK, well I suppose that you're tied to the brief.

The simplest way to get the GPA from 5 textboxes would be to do this:

$ave = ($_POST['grade1']+$_POST['grade2']+$_POST['grade3']+$_POST['grade4']+$_POST['grade5'])/5;

But of course, what if you have only 3 textboxes filled or non-integer entries? We need some sort of validation and filtering out of empty items.

There are a number of approaches that you could take. You could check each item individually - this is the approach many beginners make - because it sort of makes sense:

if($_POST)
{

    $total = 0;
    $count = 0;

    if(is_int($_POST['grade1']) && $_POST['grade1'] >= 0 && $_POST['grade1'] <= 100)
    {
        $total += $_POST['grade1'];
        $count++;
    }
    if(is_int($_POST['grade2']) && $_POST['grade2'] >= 0 && $_POST['grade2'] <= 100)
    {
        $total += $_POST['grade2'];
        $count++;
    }
    if(is_int($_POST['grade3']) && $_POST['grade3'] >= 0 && $_POST['grade3'] <= 100)
    {
        $total += $_POST['grade3'];
        $count++;
    }
    if(is_int($_POST['grade4']) && $_POST['grade4'] >= 0 && $_POST['grade4'] <= 100)
    {
        $total += $_POST['grade4'];
        $count++;
    }
    if(is_int($_POST['grade5']) && $_POST['grade5'] >= 0 && $_POST['grade5'] <= 100)
    {
        $total += $_POST['grade5'];
        $count++;
    }
    if($count)
    {
        $average = $total / $count;
    }
}

But see how verbose that is. Imagine having a 100 textboxes and writing code for that! You'd go insane. So how to go about it if your hands are tied with regards to the static form that you have to use? Well, we could place the fields into an array and use a loop:

if($_POST)
{
    $points = array($_POST['grade1'],$_POST['grade2'],$_POST['grade3'],$_POST['grade4'],$_POST['grade5']);
    $clean = array_filter($points,"is_int");  
    if(!empty(clean)) $average = array_sum($clean) / count($clean);
}

You'll notice that if you want to add another 10 textboxes - you just need to add their names to the $points array - everything else is done automatically - unlike the first example where you need to laboriously include a conditional statement (if block)

However, even this is rather strenuous - having to explicitly name every text field in the html form AND having to replicate those names in the $points array.

Imagine that you were not constrained by your professor and were allowed to make the code as flexible and as easy to maintain as possible. We could start off with something like:

$number_textboxes = 5;

if($_POST)
{
   $clean = array_filter($_POST['mark'],"is_int");  
   if(!empty(clean)) $average = array_sum($clean) / count($clean);
}

That's pretty much it. All you need to do is change the $number_textboxes variable to produce that number of form textboxes - no need to add names or anything else. The php code in the form could be this...

<form method="post">
<?php
    for($i=0;$i<$number_textboxes;$i++)
    {
        echo "<input type='number' min='0' max='100' name='mark[$i]' />";
    }
?>
    <input type="submit" value="Calculate GPA" />
</form>

When viewed in browser source view, you get...

<form method="post">
    <input type='number' min='0' max='100' name='mark[0]' />
    <input type='number' min='0' max='100' name='mark[1]' />
    <input type='number' min='0' max='100' name='mark[2]' />
    <input type='number' min='0' max='100' name='mark[3]' />
    <input type='number' min='0' max='100' name='mark[4]' />
    <input type="submit" value="Calculate GPA" />
</form>

This means that your $_POST field for all the 'mark' fields is $_POST['mark']
which is an array of $_POST['mark'][0] ... $_POST['mark'][4]

OK, I've only touched on the calculation of the GPA here, not the grade awarded, but I hope that gives you an idea.

The awarding of a grade can be done with a switch/case or an if/elseif or a foreach/if statements.

If find the foreach/if statements easier as I only have to change the structure of an array to modify elements in the grade:mark relationship - otherwise you're modifying the actual conditional (switch or if) statements again - which, as discussed above if a right PITA.

0

WOW...my head is still spinning! I swear, many of the people are way more advanced than I could ever be. It seems like you guys figure these things out in minute. I will try to figure all this out and let you know how it goes. Do all these codes go in the PHP document or do some go in the HTML document? Part of my problem is that I'm working on 5 of these scrips at the same time and I'm getting confused. I have three of them done, I just have this and the table to work on, oh wait, I have a presentation, two papers, another project in Networking and a lot of reading to do as well as a 5 year old son. What the hell was I thinking!!! Thanks you so much.

0

With regard to mixing php and html - it's useful to separate them as much as possible. I usually try to place most of the php code into a separate include php file, so that it's reusable in many pages. If that's not so appropriate, then perhaps place it above the !doctype declaration. So the only things you get mixed in with the html are a few trival 'echo $variable'.

0

Let me get this right. In the script above that has 36 lines, that is what goes in the php document between <?php and ?> and the one that has 9 rows and begins with <form goes in the html document? I've tried putting all of the scripts above in both the php and html and nothing is working. I don't know about this class! It's something I want to learn, but I'm missing something. I need a personal tutor to drill it into my head. Sorry

0

I guess my biggest issue is the 2 documents, PHP and HTML as that is part of the criteria!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.