Page 1 of 1

Problem in searching through a multi dimensional array

Posted: Tue Jun 24, 2008 4:12 pm
by adeydas
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:

Code: Select all

$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

Re: Problem in searching through a multi dimensional array

Posted: Tue Jun 24, 2008 4:37 pm
by WebbieDave
Well, I'm unwilling to actually step through your code and debug it for you (maybe someone else will more time to do so) but I would suggest that you check the values of variables at pertinent points in execution by using some well placed echo's.

For instance, you can try inserting this into line 10:

Code: Select all

echo 'if(' . $_POST[$z] . ' && ' . $nomore . '==false && in_array(' . $z . ', ' . $ans[$i]. "))<br>\n"
This helps you ensure that the values are as expected.