maxudaskin Posted May 11, 2008 Share Posted May 11, 2008 I have an online test that uses radio groups. I have an answer key as an array and I wanted to use arrays to mark it... can you either tell me a better way to do it or help me complete this simple part of it... <?php $test_key = array("c","b","a","d","d","c","c","b","d","a"); $given_ans = array($_POST['q1'],$_POST['q2'],$_POST['q3'],$_POST['q4'],$_POST['q5'],$_POST['q6'],$_POST['q7'],$_POST['q8'],$_POST['q9'],$_POST['q10']); ?> Link to comment https://forums.phpfreaks.com/topic/105095-solved-marking-a-test/ Share on other sites More sharing options...
DarkWater Posted May 11, 2008 Share Posted May 11, 2008 <?php $test_key = array("c","b","a","d","d","c","c","b","d","a"); $given_ans = array($_POST['q1'],$_POST['q2'],$_POST['q3'],$_POST['q4'],$_POST['q5'],$_POST['q6'],$_POST['q7'],$_POST['q8'],$_POST['q9'],$_POST['q10']); foreach ($given_ans as $k=>$v) { if ($v == $test_key[$k]) { $ans[] = 1; } else { $ans[] = 2; } } foreach ($ans as $k=>$v) { $num = $k+1; $response = ($v==1)?"Correct":"Incorrect"; echo $num . ": " . $response; } ?> Link to comment https://forums.phpfreaks.com/topic/105095-solved-marking-a-test/#findComment-538031 Share on other sites More sharing options...
maxudaskin Posted May 11, 2008 Author Share Posted May 11, 2008 All I want from the script is a flat mark out of ten so I can multiply it by 0.01 and get the percentage. I am going to be working with the percentage. <?php $mark = $mark / 10 * 100; ?> Link to comment https://forums.phpfreaks.com/topic/105095-solved-marking-a-test/#findComment-538033 Share on other sites More sharing options...
DarkWater Posted May 11, 2008 Share Posted May 11, 2008 <?php $test_key = array("c","b","a","d","d","c","c","b","d","a"); $given_ans = array($_POST['q1'],$_POST['q2'],$_POST['q3'],$_POST['q4'],$_POST['q5'],$_POST['q6'],$_POST['q7'],$_POST['q8'],$_POST['q9'],$_POST['q10']); foreach ($given_ans as $k=>$v) { if ($v == $test_key[$k]) { $mark += 1; } else { $mark += 0; } } echo $mark; ?> Link to comment https://forums.phpfreaks.com/topic/105095-solved-marking-a-test/#findComment-538034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.