adeydas Posted June 24, 2008 Share Posted June 24, 2008 Hi, I am trying to make an online examination script in PHP and am stuck at the results page. The actual answers here are stored in a multidimensional array called 'ans' in the format: Say for question 1 the answers are options 2 and 4, then it would be stored as 12 14 If for the second question the answers are option 1 ,3 and 4 then 21 23 24 So the whole array would look like: 12 14 21 23 24 The options are check boxes with ID's and names similar to the way it is stored in the array. I am using the following snippet of code to check through the answers: Code: $tot=0; $cq=0; for($i=1; $i<=$noq2d; $i++) { $nomore=false; $rt=false; for($j=1; $j<7; $j++) { $z=$i.$j; if($_POST[$z] && $nomore==false && in_array($z,$ans[$i])) { $rt=true; } else { $rt=false; $nomore=true; } } if($rt==true) { $tot=$tot+$mpq; $cq++; } } echo('Total: '.$tot.'<br><br>'); echo('Number of correct answers: '.$cq); However, it is giving 0 as the result under any and every situation. The full code of the result page is here: http://adeydas.com/dump/result.phps Any help will be highly appreciated! Thanks. Abhishek http://adeydas.com Link to comment https://forums.phpfreaks.com/topic/111739-problem-in-searching-through-a-multi-dimensional-array/ Share on other sites More sharing options...
Barand Posted June 24, 2008 Share Posted June 24, 2008 $mpq not defined anywhere Link to comment https://forums.phpfreaks.com/topic/111739-problem-in-searching-through-a-multi-dimensional-array/#findComment-573635 Share on other sites More sharing options...
lemmin Posted June 24, 2008 Share Posted June 24, 2008 I think it would be a lot easier for you if you used a different method. I would do something like this for the question form: <form> Question 1<br> <input type=checkbox name="0[]" value=1>Answer 1<bR> <input type=checkbox name="0[]" value=2>Answer 2<bR> <input type=checkbox name="0[]" value=3>Answer 3<br> <input type=checkbox name="0[]" value=4>Answer 4<br><br> Question 2<br> <input type=checkbox name="1[]" value=1>Answer 1<bR> <input type=checkbox name="1[]" value=2>Answer 2<bR> <input type=checkbox name="1[]" value=3>Answer 3<br> <input type=checkbox name="1[]" value=4>Answer 4<br> <input type=SUBMIT> </form> Then the PHP could be like this: $answers = array( array(2,4), array(1,3,4) ); foreach ($_GET as $index => $userAnswers) { if ($answers[$index] == $userAnswers) echo "Question $index is Right!"; else echo "Question $index is Wrong!"; } It basically builds the user imput to match the array that holds the answers. You could even do compare $answers == $_GET and it would tell if all questions are right or if one or more is wrong (only if there aren't any other get variables). You probably want to use POST, but this is just a concept. Link to comment https://forums.phpfreaks.com/topic/111739-problem-in-searching-through-a-multi-dimensional-array/#findComment-573641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.