Jump to content

PHP Grading Test Help...


mostwanted

Recommended Posts

I made a test in PHP and my grading file code is

<?
$question1 = $_POST['question1'];

if($question1 == "a"){

$score += floor((10 / 3) * 2);
}


$question2 = $_POST['question2'];


if($question2 == "b"){

$score += floor((10 / 3) * 2);
}


$question3 = $_POST['question3'];


if($question3 == "a"){

$score += floor((10 / 3) * 2);
}


$question4 = $_POST['question4'];


if($question4 == "b"){

$score += floor((10 / 3) * 2);
}


$question5 = $_POST['question5'];


if($question5 == "a"){

$score += floor((10 / 3) * 2);
}


$question6 = $_POST['question6'];


if($question6 == "b"){

$score += floor((10 / 3) * 2);
}


$question7 = $_POST['question7'];


if($question7 == "a"){

$score += floor((10 / 3) * 2);
}


$question8 = $_POST['question8'];


if($question1 == "b"){

$score += floor((10 / 3) * 2);
}


$question9 = $_POST['question9'];


if($question9 == "a"){

$score += floor((10 / 3) * 2);
}


$question10 = $_POST['question10'];


if($question10 == "a"){

$score += floor((10 / 3) * 2);
}


$question11 = $_POST['question11'];


if($question11 == "c"){

$score += floor((10 / 3) * 2);
}


$question12 = $_POST['question12'];


if($question12 == "a"){

$score += floor((10 / 3) * 2);
}


$question13 = $_POST['question13'];


if($question13 == "d"){

$score += floor((10 / 3) * 2);
}

$question14 = $_POST['question14'];


if($question14 == "d"){

$score += floor((10 / 3) * 2);
}

$question15 = $_POST['question15'];


if($question15 == "d"){

$score += floor((10 / 3) * 2);
}

$name = $_POST['fname'];
$phone = $_POST['phone'];
$street = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = "[email protected], [email protected]";
$uemail = $_REQUEST['uemail'];


mail($email, "$name just took the test!", "$name just took the test and scored a $score on the test.\n\n-Contact Info-\n\nName: $name\nPhone: $phone\nStreet: $street\nCity: $city\nState: $state\nZip: $zip\nE-Mail Address: $uemail", "From: [email protected]");

mail($uemail, "CoolAnger Test Results", "Thank you for taking the Anger Management test!\nYou scored a $score on the test!\n\nYour Contact info-\n\nName: $name\nPhone: $phone\nStreet: $street\nCity: $city\nState: $state\nZip: $zip\nE-Mail Address: $uemail", "From: [email protected]");


?>
Your results have be emailed to you.<br>
You may close this page now..<br>

You will be notified if we need any further information.<br>
<br>
Thank you!

 

What is happening is this... I take the test and I know the answers are all correct, but it scores it a 84 when it should be 100... I'm about to go crazy on this and would appreciate any help on this.. :)

Link to comment
https://forums.phpfreaks.com/topic/167332-php-grading-test-help/
Share on other sites

Yeah, print the values along the way so that you can see for yourself where it goes wrong. Also, you should store the answers in a database or at least use arrays because that code doesn't look very good  ;)

 

 

To elaborate on what he said:

 

 

 


<form action="" method="post">
What color is the sky?
<select name="question[]">
    <option value="a">A. Red</option>
    <option value="b">B. Green</option>
    <option value="c">C. Blue</option>
    <option value="d">D. Black</option>
</select>

<br>
<br>

What do dogs do?
<select name="question[]">
    <option value="a">A. Meow</option>
    <option value="b">B. Bark</option>
    <option value="c">C. Moo</option>
    <option value="d">D. Cluck</option>
</select>

</form>

 

 

 

Then PHP wise:

 

$answers = array('c', 'b');

$questions = (isset($_POST['question']) && is_array($_POST['question'])) ? $_POST['question'] : array();

$score = 0;

foreach($questions as $number => $answer) {
    if(isset($answers[$number]) && $answers[$number] == $answer) {
        //answered correctly!
        $score += 50; //50% per question
    }
}

echo "You scored {$score}/100.";

 

 

 

 

 

Oh, and it's adding not adding to 100 because floor(10/3*2) = 6....  And 15*6 != 100.

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

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