Jump to content

Calculating GPA


verdrm

Recommended Posts

If you have a table for classes with these fields:

 

Class                  NumCredits            Grade

---------------------------------------------------

Biology 532          6                          3.5        (AB)

Basket-Weaving    3                          2.0        ©

Philosophy 773      4                          4.0        (A)

 

You can get the GPA like so:

 

<?php
$link = mysql_connect(...);
mysql_select_db(...);
$tot_credits = 0;
$tmp_gpa = 0;
$res = mysql_query("SELECT * FROM classes");
while($row = mysql_fetch_assoc($res)) {
   $tmp_gpa += $row['Grade']*$row['NumCredits'];
   $tot_credits += $row['NumCredits'];
}
$gpa_final = round(($tmp_gpa/$tot_credits),3);
?>

 

Is this sort of what you wanted?

Link to comment
Share on other sites

If you had something like this:

 

student_id     class     credits      grade
------------------------------------------
1                Biology        1             4
2                Algrebra      1             3

 

You could do something like:

 

<?php
//connect
$q = mysql_query('SELECT AVG(credits*grade) FROM grades WHERE student_id = 1');
?>

 

I don't remember how to calculate a GPA, so that could be entirely wrong, but.... lol

Link to comment
Share on other sites

Ahhh.... It's all coming back now (which is odd considering I'm in highschool haha)...

 

 

Anyway, if you wanted to do the loop inside of mysql instead of PHP, you could do a query like:

 

SELECT SUM(grade*credits)/SUM(credits) AS gpa FROM Grades WHERE student_id = 1

Link to comment
Share on other sites

Does anyone know how to calculate GPA using PHP?

 

To get back to the question, the answer is that php has nothing to do with it.  GPA is calculated using standard math (see Barand's link) regardless of the scripting/programming language that's used.

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.