PHPBear Posted February 21, 2015 Share Posted February 21, 2015 I'm developing a series of database-driven quizzes and tests. Most of the answers will be multiple choice, but there will also be some fill-in-the-blank questions. What's causing me trouble is questions that ask users to choose several answers by selecting several checkboxes. There could be one or more such questions in each quiz, and the number of correct and wrong choices will vary. The answer key below is tailored for a quiz with just one checkbox question, with six choices - three correct and three incorrect. The script awards partial scores for each correct choice and also deducts points for each wrong answer. I'd like to change it so that there are no gray areas - a question is either right or wrong. In this particular example, a user has to choose all three correct answers and no correct answers. Any variation whatsoever results in a wrong answer (0% for that answer). I would guess it should be as simple as coding it to say "a checkbox answer must be exactly identical to the array within in an array in the answer key." Unfortunately, I don't have a clue how to write that. Thanks for any tips. if (isset($_POST)): $totalCorrect = 0; $answers = array(1 => 'A', 2 => 'banana', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C')); foreach ($answers as $num => $answer): $question = 'q'.$num; if(is_array($answer) && isset($_POST[$question])){ $ans_cnt = count($answer); $ans_value = (1 / $ans_cnt); $post_cnt = count($_POST[$question]); //find matches between answer array and post array $matches = array_intersect($answer,$_POST[$question]); $good_answers = count($matches); //Get bad answer count, which be be subtracted from overall score $bad_answers = 0; foreach($_POST[$question] as $post_answer): if(!in_array($post_answer,$answer)): $bad_answers++; endif; endforeach; //Result of good answers minus bad answers // $result = $good_answers - $bad_answers; if($good_answers > $bad_answers){ $result = $good_answers - $bad_answers; }else{ $result = 0; } if(($post_cnt != $ans_cnt) || ($post_cnt == $ans_cnt && $ans_cnt != count($matches))){ $result = $result * $ans_value; $totalCorrect = $totalCorrect + $result; }else{ $totalCorrect++; } }elseif(isset($_POST[$question]) && strtolower($_POST[$question]) === strtolower($answer)){ $totalCorrect++; } endforeach; $pct = round( (($totalCorrect/count($answers)) * 100), 0); echo $totalCorrect.' correct ('.$pct.'%)'; endif; Quote Link to comment https://forums.phpfreaks.com/topic/294761-checkbox-array-in-answer-key-array/ Share on other sites More sharing options...
davidannis Posted February 21, 2015 Share Posted February 21, 2015 You already count good and bad answers and then score them here: if($good_answers > $bad_answers){ $result = $good_answers - $bad_answers; }else{ $result = 0; } Why not just change the scoring to? if($good_answers ==3 && $bad_answers==0){ $result = 1; }else{ $result = 0; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/294761-checkbox-array-in-answer-key-array/#findComment-1506294 Share on other sites More sharing options...
PHPBear Posted February 21, 2015 Author Share Posted February 21, 2015 (edited) You already count good and bad answers and then score them here: if($good_answers > $bad_answers){ $result = $good_answers - $bad_answers; }else{ $result = 0; } Why not just change the scoring to? if($good_answers ==3 && $bad_answers==0){ $result = 1; }else{ $result = 0; } That works great for this particular example. Thanks. However, what happens if I there are TWO questions with multiple checkbox answers, one with three correct answers, the other with five? That's why I'd like to take it one step further and "automate" it so that the script automatically handles all checkbox questions, regardless of the number of correct answers. So the code might look something like this: if($good_answers ==[the total number of correct choices in a given question] && $bad_answers==0) If it helps, here's the HTML for two questions - a multiple choice question and a checkbox question: <li id="q9"> <div class="Question">Scientists believe the universe is:</div> <div class="Answer"> <label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div> <div class="Answer"> <label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div> <div class="Answer"> <label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9" id="q9-C" value="C" style="display: none;"> C. contracting</div></label></div> <div class="Answer"> <label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9" id="q9-D" value="D" style="display: none;"> D. becoming bipolar</div></label></div> </li> <li id="q10"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label> <label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label> <label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label> <label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label> <label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label> <label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label> </div> </li> Edited February 21, 2015 by PHPBear Quote Link to comment https://forums.phpfreaks.com/topic/294761-checkbox-array-in-answer-key-array/#findComment-1506295 Share on other sites More sharing options...
Solution tryingtolearn Posted February 22, 2015 Solution Share Posted February 22, 2015 This is how I've handled this in the past. Using your last example (Well the answers aren't correct its just a mix of different combinations) just change your form element id's You should be able to adjust the questions how you need from fill in the blank, multiple choice, and checkboxes. Hope it helps <!DOCTYPE html> <html lang="en"> <head> <meta charset=utf-8> <title>test</title> </head> <body> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $qa = $_POST['q']; $correct = 0; $answers = array(1 => array('A','B'), 2 => array('A','B','C'), 3 => array('A'), 4 => array('A','B','C','D'), 5 => array('A','B'), 6 => array('A','B'), 7 => 'cat', 8 => 'B', 9 => 'C', 10 => array('A','B')); $total = count($answers); foreach($answers as $k => $v){ if(is_array($v)){ if($qa[$k] === $answers[$k]){ $correct++; } }else{ if($qa[$k] === $answers[$k]){ $correct++; } } } $grade= ($correct/count($answers))*100; echo"<p>Score $grade %</p>"; } ?> <form action="index.php" method="post"> <ul> <li id="q1"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q1-A"><input type="checkbox" name="q[1][]" id="q1-A" value="A" >planet</label> <label for="q1-B"><input type="checkbox" name="q[1][]" id="q1-B" value="B" >asteroid</label> <label for="q1-C"><input type="checkbox" name="q[1][]" id="q1-C" value="C" >comet</label> <label for="q1-D"><input type="checkbox" name="q[1][]" id="q1-D" value="D" >black hole</label> <label for="q1-E"><input type="checkbox" name="q[1][]" id="q1-E" value="E">neutrino star</label> <label for="q1-F"><input type="checkbox" name="q[1][]" id="q1-F" value="F">quasar</label> </div> </li> <li id="q2"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q2-A"><input type="checkbox" name="q[2][]" id="q2-A" value="A" >planet</label> <label for="q2-B"><input type="checkbox" name="q[2][]" id="q2-B" value="B" >asteroid</label> <label for="q2-C"><input type="checkbox" name="q[2][]" id="q2-C" value="C" >comet</label> <label for="q2-D"><input type="checkbox" name="q[2][]" id="q2-D" value="D">black hole</label> <label for="q2-E"><input type="checkbox" name="q[2][]" id="q2-E" value="E">neutrino star</label> <label for="q2-F"><input type="checkbox" name="q[2][]" id="q2-F" value="F">quasar</label> </div> </li> <li id="q3"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q3-A"><input type="checkbox" name="q[3][]" id="q3-A" value="A" >planet</label> <label for="q3-B"><input type="checkbox" name="q[3][]" id="q3-B" value="B" >asteroid</label> <label for="q3-C"><input type="checkbox" name="q[3][]" id="q3-C" value="C" >comet</label> <label for="q3-D"><input type="checkbox" name="q[3][]" id="q3-D" value="D">black hole</label> <label for="q3-E"><input type="checkbox" name="q[3][]" id="q3-E" value="E">neutrino star</label> <label for="q3-F"><input type="checkbox" name="q[3][]" id="q3-F" value="F">quasar</label> </div> </li> <li id="q4"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q4-A"><input type="checkbox" name="q[4][]" id="q4-A" value="A" >planet</label> <label for="q4-B"><input type="checkbox" name="q[4][]" id="q4-B" value="B" >asteroid</label> <label for="q4-C"><input type="checkbox" name="q[4][]" id="q4-C" value="C" >comet</label> <label for="q4-D"><input type="checkbox" name="q[4][]" id="q4-D" value="D">black hole</label> <label for="q4-E"><input type="checkbox" name="q[4][]" id="q4-E" value="E">neutrino star</label> <label for="q4-F"><input type="checkbox" name="q[4][]" id="q4-F" value="F">quasar</label> </div> </li> <li id="q5"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q5-A"><input type="checkbox" name="q[5][]" id="q5-A" value="A" >planet</label> <label for="q5-B"><input type="checkbox" name="q[5][]" id="q5-B" value="B" >asteroid</label> <label for="q5-C"><input type="checkbox" name="q[5][]" id="q5-C" value="C" >comet</label> <label for="q5-D"><input type="checkbox" name="q[5][]" id="q5-D" value="D">black hole</label> <label for="q5-E"><input type="checkbox" name="q[5][]" id="q5-E" value="E">neutrino star</label> <label for="q5-F"><input type="checkbox" name="q[5][]" id="q5-F" value="F">quasar</label> </div> </li> <li id="q6"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q6-A"><input type="checkbox" name="q[6][]" id="q6-A" value="A" >planet</label> <label for="q6-B"><input type="checkbox" name="q[6][]" id="q6-B" value="B" >asteroid</label> <label for="q6-C"><input type="checkbox" name="q[6][]" id="q6-C" value="C" >comet</label> <label for="q6-D"><input type="checkbox" name="q[6][]" id="q6-D" value="D">black hole</label> <label for="q6-E"><input type="checkbox" name="q[6][]" id="q6-E" value="E">neutrino star</label> <label for="q6-F"><input type="checkbox" name="q[6][]" id="q6-F" value="F">quasar</label> </div> </li> <li id="q7"> <div class="Question">Scientists believe the universe is:</div> <div class="Answer"> <label class="Wide" for="q7-A"><div class="Radio"><input type="text" name="q[7]" id="q7-A" value="" style="display: inline;"></div></label> </div> </li> <li id="q8"> <div class="Question">Scientists believe the universe is:</div> <div class="Answer"> <label class="Wide" for="q8-A"><div class="Radio"><input type="radio" name="q[8]" id="q8-A" value="A" style="display: inline;"> A.disappearing</div></label> </div> <div class="Answer"> <label class="Wide" for="q8-B"><div class="Radio"><input type="radio" name="q[8]" id="q8-B" value="B" style="display: inline;" > B. expanding</div></label> </div> <div class="Answer"> <label class="Wide" for="q8-C"><div class="Radio"><input type="radio" name="q[8]" id="q8-C" value="C" style="display: inline;"> C. contracting</div></label> </div> <div class="Answer"> <label class="Wide" for="q8-D"><div class="Radio"><input type="radio" name="q[8]" id="q8-D" value="D" style="display: inline;"> D. becoming</div></label> </div> </li> <li id="q9"> <div class="Question">Scientists believe the universe is:</div> <div class="Answer"> <label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q[9]" id="q9-A" value="A" style="display: inline;"> A.disappearing</div></label> </div> <div class="Answer"> <label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q[9]" id="q9-B" value="B" style="display: inline;" > B. expanding</div></label> </div> <div class="Answer"> <label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q[9]" id="q9-C" value="C" style="display: inline;"> C. contracting</div></label> </div> <div class="Answer"> <label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q[9]" id="q9-D" value="D" style="display: inline;"> D. becoming</div></label> </div> </li> <li id="q10"> <div class="Question">Check each item that can be found in our solar system.</div> <div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;"> <label for="q10-A"><input type="checkbox" name="q[10][]" id="q10-A" value="A" >planet</label> <label for="q10-B"><input type="checkbox" name="q[10][]" id="q10-B" value="B" >asteroid</label> <label for="q10-C"><input type="checkbox" name="q[10][]" id="q10-C" value="C" >comet</label> <label for="q10-D"><input type="checkbox" name="q[10][]" id="q10-D" value="D">black hole</label> <label for="q10-E"><input type="checkbox" name="q[10][]" id="q10-E" value="E">neutrino star</label> <label for="q10-F"><input type="checkbox" name="q[10][]" id="q10-F" value="F">quasar</label> </div> </li> </ul> <input type="submit" name="submit" value="submit" /> </form> </body> </html> 1 Quote Link to comment https://forums.phpfreaks.com/topic/294761-checkbox-array-in-answer-key-array/#findComment-1506443 Share on other sites More sharing options...
PHPBear Posted February 23, 2015 Author Share Posted February 23, 2015 Awesome! I asked this question @ StackOverflow some time ago, and no one could answer it. So I finally decided to try another forum and struck gold. Quote Link to comment https://forums.phpfreaks.com/topic/294761-checkbox-array-in-answer-key-array/#findComment-1506489 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.