mostwanted Posted July 24, 2009 Share Posted July 24, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/ Share on other sites More sharing options...
ldougherty Posted July 25, 2009 Share Posted July 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/#findComment-882406 Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/#findComment-882996 Share on other sites More sharing options...
corbin Posted July 27, 2009 Share Posted July 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/#findComment-884169 Share on other sites More sharing options...
gizmola Posted July 27, 2009 Share Posted July 27, 2009 Yeah it seems to me a simpler way of deriving the grade would be: Start with $correct = 0; For each case $correct++; At the end, $score = round(($correct*100)/15); Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/#findComment-884277 Share on other sites More sharing options...
infiniteacuity Posted August 11, 2009 Share Posted August 11, 2009 Yeah it seems to me a simpler way of deriving the grade would be: Start with $correct = 0; For each case $correct++; At the end, $score = round(($correct*100)/15); I think this is the best and most efficient way to go. Quote Link to comment https://forums.phpfreaks.com/topic/167332-php-grading-test-help/#findComment-895634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.