Creating a level of numbers for a pass scoring system

时间:2015-06-26 10:03:39

标签: php json

I have a set of values from a json_decode and they look like this the number being the row 'pass_levels' and the letter being the row 'pass_level_name'.

90 -> A, 80 -> B, 70 -> C

I have a score that if it is above or below a certain number I want it to show the letter. So if I score 85 it shows a B. The problem I have with the code below is that if the score is above any of the numbers it shows a C regardless.

I know I could do if > 90 echo A but the variables are not set and the amount of variables could change. So it could change and the variables might be:

A -> 95, B -> 90, C -> 80, D -> 70, E -> 60, F -> 50.

In all circumstances anything scored below the lowest number would be a fail.

$score = $row['score'];


$levels = json_decode($row['pass_levels']);
$levels_name = json_decode($row['pass_level_name']);

foreach(array_combine($levels, $levels_name) as $level => $level_name){                 
}

if($score >= $level){ echo $score.'% '.$level_name; } else { echo "Fail"; }

I hope I've explained this clearly enough for you to help me.

Thanks

0 个答案:

没有答案