0
<?php 
$ans=$_REQUEST['ans'];
$qes=$_REQUEST['qes'];
$ra=$_REQUEST['right_op'];
$count=0;
for($i=0; $i<count($ans); $i++)
{
    echo "Question".$qes[$i]"<br>";
    echo "Ans".$ans[$i]"<br>";
    echo "Right Option".$ra[$i]."<br>";
    if(isset($ans[$i]) == isset($ra[$i]))
    {
        $count++;
    }
}
?>

when submit the first page, then it shows

Notice: Undefined offset: 1 in C:\xampp\htdocs\result.php on line 9

The first page where the data is posted.

<?php 
$i=1;
$x=0;
$y=0;
$z=0;

do{
  ?>
            <tr>
              <td width="30"></td>
              <td width="30" height="27"><?php echo "$i";?></td>
              <td width="493"><?php echo $row_question['question']; ?>
                <input type="hidden" name="q_id[<?php// echo $y; ?>]" id="q_id" value="    <?php echo $row_question['q_id']; ?>" />
                <input type="hidden" name="qes[<?php echo $y; ?>]" id="qes" value="<?php     echo $row_question['question']; ?>" /><input name="right_op[<?php echo $z; ?>]"     type="hidden" id="right_op" value="<?php echo $row_question['right_op']; ?>" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td height="59" align="right"><h3>A)</h3>
                <h3>B)</h3>
                <h3>C)</h3>
                <h3>D)</h3></td>
              <td><h3>
                <label>
                  <input type="radio" name="ans[<?php echo $x; ?>]" value="A" id="ans_0"     />
                      <font color="#FFFFFF"><?php echo $row_question['op_a']; ?></font>    </label>
                <br />
                <label>
                  <input type="radio" name="ans[<?php echo $x; ?>]" value="B" id="ans_1"     />
                  <font color="#FFFFFF"><?php echo $row_question['op_b']; ?></font>    </label>
                <br />
                <label>
                  <input type="radio" name="ans[<?php echo $x; ?>]" value="C" id="ans_2"     />
                  <font color="#FFFFFF"><?php echo $row_question['op_c']; ?></font>    </label>
                <br />
                <label>
                   <input type="radio" name="ans[<?php echo $x; ?>]" value="D" id="ans_3"     /> 
                  <font color="#FFFFFF"><?php echo $row_question['op_d']; ?></font>    </label>
                <br />
              </h3></td>
             </tr>

<tr>
  <td height="17" colspan="3"><hr /></td>
    </tr><?php
    $i++; 
    $x++;
    $y++;
        $z++;
    } while ($row_question = mysql_fetch_assoc($question)); ?>

What is wrong in this code. Please tell me in details.
Thank You.

Subhajit
  • 390
  • 3
  • 18
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – mario Jul 06 '13 at 18:09

3 Answers3

2

Always use isset() before access

if (isset($ra[$i])  && isset($ans[$i]) && isset($qes[$i])){
  // your code
}

or

array_key_exists($i, $ra);

or

Nick
  • 4,192
  • 1
  • 19
  • 30
0

It looks like you expect all arrays to be the same size, which is not the case for $ra which is snorter than other arrays, therefore

 echo "Right Option".$ra[$i]."<br>";

causes the notice. Looks like you need to fix your $ra size.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

$ra doesn't have as many members in the array as $ans does, so when you hit the 2nd iteration of the loop there's nothing in $ra[1] to display. You need to figure out why the length of $ra and $ans are different - without more information about what you're doing it's difficult for us to know.

As @Nikola indicates, you should always check to make sure there's something in the index you're accessing before trying to output it using isset(). Whether or not it's valid for there not to be something in $ra[1] when there is something in $ans[1] is something only you can answer.

Inferring a lot from your code, it looks as though $ra is intended to indicate the right answer. In that case there's only going to be one, so you should use $ra[0] instead of $ra[$i], which will always show the first element in the $ra array. Of course I may be misinterpreting the intent of your code, so YMMV.

cori
  • 8,666
  • 7
  • 45
  • 81