Jump to content

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 = "john.r.diaz@gmail.com, hayesjoey@gmail.com";
$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: info@coolanger.com");

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: info@coolanger.com");


?>
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

The only way to truly determine what is happening would to be look at the variable output as the script is executing.

 

I suggest as your computing the grade that you keep echoing out the actual variable value to figure out where it is not computing the way you expect.

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