OK here is a solution for MySQL, as it is probably the most frequently used database among PHP scripts:
SET @myClass:=0; SET @myRank:= 0; SET @myTotals:= 0; SELECT X.*, @myRank:=CASE WHEN @myClass <> class THEN 1 WHEN @myTotals <> totals THEN @myRank+1 ELSE @myRank END AS Rank, @myClass:=class AS myClass, @myTotals:=totals AS myTotals FROM ( SELECT studentId, Math, English, Science, Class, Math + English + Science AS totals FROM studentGrades ORDER BY 5 ASC, 6 DESC ) X
gives:
studentId Math English Science Class totals Rank myClass myTotals 2 75 83 84 3p1 242 1 3p1 242 5 88 77 77 3p1 242 1 3p1 242 1 80 66 85 3p1 231 2 3p1 231 6 92 97 96 5p2 285 1 5p2 285 3 70 88 90 5p2 248 2 5p2 248 4 50 82 50 5p2 182 3 5p2 182
no spaces in the ranks
EDIT
If you want follow-up student positions to be maintained after several students with the same rank (e.g. 1,2,2,4, not 1,2,2,3), use the following:
SET @myClass:=0; SET @myRank:= 0; SET @myNextRank:= 0; SET @myTotals:= 0; SELECT X.*, @myRank:=CASE WHEN @myClass <> class THEN @myNextRank:=1 WHEN @myTotals <> totals THEN @myRank: =@myNextRank ELSE @myRank END AS Rank, @myClass:=class AS myClass, @myTotals:=totals AS myTotals, @myNextRank: =@myNextRank +1 AS myNextRank FROM ( SELECT studentId, Math, English, Science, Class, Math + English + Science AS totals FROM studentGrades ORDER BY 5 ASC, 6 DESC ) X
gives
studentId Math English Science Class totals Rank myClass myTotals myNextRank 2 75 83 84 3p1 242 1 3p1 242 2 5 88 77 77 3p1 242 1 3p1 242 3 1 80 66 85 3p1 231 3 3p1 231 4 6 92 97 96 5p2 285 1 5p2 285 2 3 70 88 90 5p2 248 2 5p2 248 3 4 50 82 50 5p2 182 3 5p2 182 4
with spaces in the ranks