tags:

views:

54

answers:

1

I have this php that scores a test based on hourly answers:

    $score_a = 0;

    foreach(array(4,5,10) as $a){
        if ($a >= 2 && $a <= 4) {
            $score_a += 1;
        } else if ($a > 4 && $a <= 8) {
            $score_a += 3;
        } else if ($a > 8) {
            $score_a += 0;
        }
    };

I need the final "else if" to score a bit differently. Instead of adding .5 once if the value is >8, I need it to add .5 for each whole number above 8.

So this score needs to be 5 and not 4.5.

+2  A: 

You mean like: $score_a += floor($a - 8) * .5; ?

You can use it like so:

foreach(array(4,5,10) as $a){
    if ($a >= 2 && $a <= 4) {
        $score_a += 1;
    } else if ($a > 4 && $a <= 8) {
        $score_a += 3;
    } else if ($a > 8) {
        $score_a += floor($a - 8) * .5;
    }
};
K Prime
I think so? I'm a major beginner...Basically, for each number above 8, .5 needs to be added (unlike the previous, which are single additions).
Kevin Brown
@Kevin - Ok, so does the (updated) code sample work for you?
K Prime
Will test tomorrow. It's 4 AM here... :)
Kevin Brown
elseif ( $a >= 9 ) { //K Prime's code goes here }must work.
Sejanus
That's great! Thanks!
Kevin Brown