Jump to content

How can I converts marks to grades in php


Olumide
 Share

Recommended Posts

Am working on a project such that if I enter scores for each subjects, it will post to the database with the grades and comment. My form will only accept value for scores which will determine the grades for each subjects and hence post to the database. Like in Microsoft Excel whereby you have data (integers) and you instruct the excel (lets say you have your value in cell A1, and Cell A2 contains the formula: if(A1>=90,"A",if(A1>=80,"B","F")). I used switch case for the grade in my php code written below.

  

     {
            
            $marks=array();
        $class=$_POST['class'];
        $studentid=$_POST['studentid']; 
        $mark=$_POST['marks'];
        $grades=$_POST['grades'];
        $comments=$_POST['comments'];
        
         $stmt = $dbh->prepare("SELECT subjects.SubName,subjects.id FROM subjectlist join  subjects on  subjects.id=subjectlist.SubjectId WHERE subjectlist.ClassId=:cid order by subjects.SubName");
         $stmt->execute(array(':cid' => $class));
          $sid1=array();
         while($row=$stmt->fetch(PDO::FETCH_ASSOC))
         {
        
        array_push($sid1,$row['id']);
           } 
          
        for($i=0;$i<count($mark);$i++){
            $mar=$mark[$i];
          $sid=$sid1[$i];
           
        $totalmarks=$marks*10;
        
        $sql="INSERT INTO  studresult(StudentId,ClassId,SubjectId,marks,grades,comments) VALUES(:studentid,:class,:sid,:marks,:grades,:comments)";
        $query = $dbh->prepare($sql);
        $query->bindParam(':studentid',$studentid,PDO::PARAM_STR);
        $query->bindParam(':class',$class,PDO::PARAM_STR);
        $query->bindParam(':sid',$sid,PDO::PARAM_STR);
        $query->bindParam(':marks',$mar,PDO::PARAM_STR);
        $query->bindParam(':grades',$grades,PDO::PARAM_STR);
        $query->bindParam(':comments',$comments,PDO::PARAM_STR);
        $query->execute();
        $lastId = $dbh->lastId();
        if($lastId)
        {
        $msg="Result successfully added";
        }
        else 
        {
        $error=" Please try again";
        }
        }
        }
        ?>
        
          <!-- My Grading starts from here -->
            <?php
        
        switch(true) {
        case $totalmarks>=90 && $totalmarks<101:
                $grade = 'A*';
                $comment='Congratulation!';
                break;
            case $totalmarks>=85 && $totalmarks<90:
                $grade = 'A1';
                $comment='Distinction';
                break;
            case $totalmarks>=80 && $totalmarks<85:
                $grade = 'B2';
                $comment='Very Good';
                break;
            case $totalmarks>=75 && $totalmarks<80:
                $grade = 'B3';
                $comment='Good';
                break;
            case $totalmarks>=70 && $totalmarks<75:
                $grade = 'C4';
                $comment='Credit';
                break;
            case $totalmarks>=65 && $totalmarks<70:
                $grade = 'C5';
                $comment='Credit';
                break;
            case $totalmarks>=60 && $totalmarks<65:
                $grade = 'C6';
                 $comment='Credit';
                break;
            case $totalmarks>=55 && $totalmarks<60:
                $grade = 'D7';
                 $comment='Pass';
                break;
            case $totalmarks>=50 && $totalmarks<55:
                $grade = 'E8';
                 $comment='Pass';
                break;
            case $totalmarks>=40 && $totalmarks<50:
                $grade = 'F9';
                 $comment='Fail';
                break;
            default:
                $grade = 'F9';
                $comment='Ungraded';
                break;
            }
        
        
        ?>
        <!-- grading stop here -->

Link to comment
Share on other sites

Just now, Barand said:

And your question is what?

My question is: how can I do it such that after entering scores for each subject, the grades will be automatically calculated and post to the database.

Link to comment
Share on other sites

Posted (edited)

You don't.

Store marks in the result table EG

+---------+-----------+-------+
| pupilid | subjectid | pcent |
+---------+-----------+-------+
|      19 |         9 |    79 |
|      24 |         5 |    86 |
|      12 |         2 |    83 |
|      14 |         1 |    79 |
|      24 |         8 |    93 |
|      20 |         1 |    81 |
|      13 |         4 |    62 |
|      10 |         4 |    90 |
|      13 |         1 |    93 |
|       5 |         8 |    95 |
+---------+-----------+-------+

create a "grading" table EG

+------------+--------+--------+-------+-----------------+           CREATE TABLE grading (
| grading_id | lomark | himark | grade | comment         |           grading_id int not null auto_increment primary key,
+------------+--------+--------+-------+-----------------+           lomark int,
|          1 |      0 |     39 | F9    | Ungraded        |           himark int,
|          2 |     40 |     49 | F9    | Fail            |           grade char(2),
|          3 |     50 |     54 | E8    | Pass            |           comment varchar(20)
|          4 |     55 |     59 | D7    | Pass            |           );
|          5 |     60 |     64 | C6    | Credit          |
|          6 |     65 |     69 | C5    | Credit          |           INSERT INTO grading (lomark, himark, grade, comment) values
|          7 |     70 |     74 | C4    | Credit          |           ( 0, 39, 'F9', 'Ungraded'),
|          8 |     75 |     79 | B3    | Good            |           (40, 49, 'F9', 'Fail'),
|          9 |     80 |     84 | B2    | Very good       |           (50, 54, 'E8', 'Pass'),
|         10 |     85 |     89 | A1    | Distinction     |           (55, 59, 'D7', 'Pass'),
|         11 |     90 |    100 | A*    | Congratulations |           (60, 64, 'C6', 'Credit'),
+------------+--------+--------+-------+-----------------+           (65, 69, 'C5', 'Credit'),
                                                                     (70, 74, 'C4', 'Credit'),
                                                                     (75, 79, 'B3', 'Good'),
                                                                     (80, 84, 'B2', 'Very good'),
                                                                     (85, 89, 'A1', 'Distinction'),
                                                                     (90,100, 'A*', 'Congratulations');

A joined query does the rest.

select pupilid
     , subjectid
     , pcent 
     , grade
     , comment
from result 
     join
     grading on pcent between lomark and himark

+---------+-----------+-------+-------+-----------------+
| pupilid | subjectid | pcent | grade | comment         |
+---------+-----------+-------+-------+-----------------+
|       1 |         1 |    78 | B3    | Good            |
|       3 |         1 |    58 | D7    | Pass            |
|       4 |         1 |    54 | E8    | Pass            |
|       8 |         1 |    91 | A*    | Congratulations |
|       9 |         1 |    62 | C6    | Credit          |
|      11 |         1 |    77 | B3    | Good            |
|      13 |         1 |    60 | C6    | Credit          |
|      14 |         1 |    64 | C6    | Credit          |
|      15 |         1 |    93 | A*    | Congratulations |
|      18 |         1 |    87 | A1    | Distinction     |
|      19 |         1 |    60 | C6    | Credit          |
|      20 |         1 |    81 | B2    | Very good       |
|       1 |         2 |    87 | A1    | Distinction     |
|       2 |         2 |    53 | E8    | Pass            |
|       4 |         2 |    87 | A1    | Distinction     |
|      12 |         2 |    93 | A*    | Congratulations |
|      14 |         2 |    62 | C6    | Credit          |

 

Edited by Barand
Link to comment
Share on other sites

On 5/15/2022 at 6:44 PM, Barand said:

You don't.

Store marks in the result table EG

+---------+-----------+-------+
| pupilid | subjectid | pcent |
+---------+-----------+-------+
|      19 |         9 |    79 |
|      24 |         5 |    86 |
|      12 |         2 |    83 |
|      14 |         1 |    79 |
|      24 |         8 |    93 |
|      20 |         1 |    81 |
|      13 |         4 |    62 |
|      10 |         4 |    90 |
|      13 |         1 |    93 |
|       5 |         8 |    95 |
+---------+-----------+-------+

create a "grading" table EG

+------------+--------+--------+-------+-----------------+           CREATE TABLE grading (
| grading_id | lomark | himark | grade | comment         |           grading_id int not null auto_increment primary key,
+------------+--------+--------+-------+-----------------+           lomark int,
|          1 |      0 |     39 | F9    | Ungraded        |           himark int,
|          2 |     40 |     49 | F9    | Fail            |           grade char(2),
|          3 |     50 |     54 | E8    | Pass            |           comment varchar(20)
|          4 |     55 |     59 | D7    | Pass            |           );
|          5 |     60 |     64 | C6    | Credit          |
|          6 |     65 |     69 | C5    | Credit          |           INSERT INTO grading (lomark, himark, grade, comment) values
|          7 |     70 |     74 | C4    | Credit          |           ( 0, 39, 'F9', 'Ungraded'),
|          8 |     75 |     79 | B3    | Good            |           (40, 49, 'F9', 'Fail'),
|          9 |     80 |     84 | B2    | Very good       |           (50, 54, 'E8', 'Pass'),
|         10 |     85 |     89 | A1    | Distinction     |           (55, 59, 'D7', 'Pass'),
|         11 |     90 |    100 | A*    | Congratulations |           (60, 64, 'C6', 'Credit'),
+------------+--------+--------+-------+-----------------+           (65, 69, 'C5', 'Credit'),
                                                                     (70, 74, 'C4', 'Credit'),
                                                                     (75, 79, 'B3', 'Good'),
                                                                     (80, 84, 'B2', 'Very good'),
                                                                     (85, 89, 'A1', 'Distinction'),
                                                                     (90,100, 'A*', 'Congratulations');

A joined query does the rest.

select pupilid
     , subjectid
     , pcent 
     , grade
     , comment
from result 
     join
     grading on pcent between lomark and himark

+---------+-----------+-------+-------+-----------------+
| pupilid | subjectid | pcent | grade | comment         |
+---------+-----------+-------+-------+-----------------+
|       1 |         1 |    78 | B3    | Good            |
|       3 |         1 |    58 | D7    | Pass            |
|       4 |         1 |    54 | E8    | Pass            |
|       8 |         1 |    91 | A*    | Congratulations |
|       9 |         1 |    62 | C6    | Credit          |
|      11 |         1 |    77 | B3    | Good            |
|      13 |         1 |    60 | C6    | Credit          |
|      14 |         1 |    64 | C6    | Credit          |
|      15 |         1 |    93 | A*    | Congratulations |
|      18 |         1 |    87 | A1    | Distinction     |
|      19 |         1 |    60 | C6    | Credit          |
|      20 |         1 |    81 | B2    | Very good       |
|       1 |         2 |    87 | A1    | Distinction     |
|       2 |         2 |    53 | E8    | Pass            |
|       4 |         2 |    87 | A1    | Distinction     |
|      12 |         2 |    93 | A*    | Congratulations |
|      14 |         2 |    62 | C6    | Credit          |

 

Thanks for your response, but is there a way I can do it without having my low, high marks already in the database but have it in my code?

 

Link to comment
Share on other sites

Why would you NOT want it in a database table?  Having it in code means the user has to have access to your code in order to make changes.  Having it in a db means you would only have to have an authorized user who can make necessary improvements getting at the values.   Modifying code or modifying data?  The better choice is always data until you begin talking about algorithmic changes.

Link to comment
Share on other sites

17 hours ago, ginerjm said:

Why would you NOT want it in a database table?  Having it in code means the user has to have access to your code in order to make changes.  Having it in a db means you would only have to have an authorized user who can make necessary improvements getting at the values.   Modifying code or modifying data?  The better choice is always data until you begin talking about algorithmic changes.

Thanks Boss, I have created the grading table as requested. But please what is now the php code to select the grades to assign to any score entered and post it to the result table. You have shown me the sql codes, but the php to perform the main task sir.

Link to comment
Share on other sites

16 hours ago, Olumide said:

Thanks for your response, but is there a way I can do it without having my low, high marks already in the database but have it in my code?

 

Thanks Boss, I have created the grading table as requested. But please what is now the php code to select the grades to assign to any score entered and post it to the result table. You have shown me the sql codes, but the php to perform the main task sir.

Link to comment
Share on other sites

On 5/15/2022 at 6:44 PM, Barand said:

You don't.

Store marks in the result table EG

+---------+-----------+-------+
| pupilid | subjectid | pcent |
+---------+-----------+-------+
|      19 |         9 |    79 |
|      24 |         5 |    86 |
|      12 |         2 |    83 |
|      14 |         1 |    79 |
|      24 |         8 |    93 |
|      20 |         1 |    81 |
|      13 |         4 |    62 |
|      10 |         4 |    90 |
|      13 |         1 |    93 |
|       5 |         8 |    95 |
+---------+-----------+-------+

create a "grading" table EG

+------------+--------+--------+-------+-----------------+           CREATE TABLE grading (
| grading_id | lomark | himark | grade | comment         |           grading_id int not null auto_increment primary key,
+------------+--------+--------+-------+-----------------+           lomark int,
|          1 |      0 |     39 | F9    | Ungraded        |           himark int,
|          2 |     40 |     49 | F9    | Fail            |           grade char(2),
|          3 |     50 |     54 | E8    | Pass            |           comment varchar(20)
|          4 |     55 |     59 | D7    | Pass            |           );
|          5 |     60 |     64 | C6    | Credit          |
|          6 |     65 |     69 | C5    | Credit          |           INSERT INTO grading (lomark, himark, grade, comment) values
|          7 |     70 |     74 | C4    | Credit          |           ( 0, 39, 'F9', 'Ungraded'),
|          8 |     75 |     79 | B3    | Good            |           (40, 49, 'F9', 'Fail'),
|          9 |     80 |     84 | B2    | Very good       |           (50, 54, 'E8', 'Pass'),
|         10 |     85 |     89 | A1    | Distinction     |           (55, 59, 'D7', 'Pass'),
|         11 |     90 |    100 | A*    | Congratulations |           (60, 64, 'C6', 'Credit'),
+------------+--------+--------+-------+-----------------+           (65, 69, 'C5', 'Credit'),
                                                                     (70, 74, 'C4', 'Credit'),
                                                                     (75, 79, 'B3', 'Good'),
                                                                     (80, 84, 'B2', 'Very good'),
                                                                     (85, 89, 'A1', 'Distinction'),
                                                                     (90,100, 'A*', 'Congratulations');

A joined query does the rest.

select pupilid
     , subjectid
     , pcent 
     , grade
     , comment
from result 
     join
     grading on pcent between lomark and himark

+---------+-----------+-------+-------+-----------------+
| pupilid | subjectid | pcent | grade | comment         |
+---------+-----------+-------+-------+-----------------+
|       1 |         1 |    78 | B3    | Good            |
|       3 |         1 |    58 | D7    | Pass            |
|       4 |         1 |    54 | E8    | Pass            |
|       8 |         1 |    91 | A*    | Congratulations |
|       9 |         1 |    62 | C6    | Credit          |
|      11 |         1 |    77 | B3    | Good            |
|      13 |         1 |    60 | C6    | Credit          |
|      14 |         1 |    64 | C6    | Credit          |
|      15 |         1 |    93 | A*    | Congratulations |
|      18 |         1 |    87 | A1    | Distinction     |
|      19 |         1 |    60 | C6    | Credit          |
|      20 |         1 |    81 | B2    | Very good       |
|       1 |         2 |    87 | A1    | Distinction     |
|       2 |         2 |    53 | E8    | Pass            |
|       4 |         2 |    87 | A1    | Distinction     |
|      12 |         2 |    93 | A*    | Congratulations |
|      14 |         2 |    62 | C6    | Credit          |

 

Thanks Boss, I have created the grading table as requested. But please what is now the php code to select the grades to assign to any score entered and post it to the result table. You have shown me the sql codes, but the php to perform the main task sir.

Link to comment
Share on other sites

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.

 Share

×
×
  • 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.