By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Undefined offset: 2 /Undefined offset: 1 error on line 14

P: 1
this code runs a para type multi choice question.there is 3 question with 4 options each.

if we select all three uestion's answer then there is no error.

if select only one question's answer Undefined offset: 1 error occurs.

if we select any two question's answer Undefined offset: 2 error occurs.












Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     $options = json_decode($question->answers); 
  3.     $correct_answers = json_decode($question->correct_answers);
  4.  
  5.      $outer_index=0;
  6.  
  7. ?> <div class="row"> <div class="col-md-12"> <ul class="questions-container fullwidth"> <?php foreach($options as $option) { 
  8.             $cAnswer = $correct_answers[$outer_index]->answer;
  9.             $uAnswer = $user_answers[$outer_index++];
  10.              //dd($uAnswer);
  11.              //print_r($option->options[0]);
  12.             $sub_options = (array)$option->options;
  13.  
  14.             foreach($sub_options as $key => $value)
  15.              $sub_options = $value;
  16.             ?> <li> <div class="question"> <h3>{{ $option->question }}</h3> </div> <div class="select-answer"> <ul class="row"> <?php $index=0; 
  17.  
  18.                      foreach($sub_options as $key => $value) { 
  19.                         $correct_answer_class = '';
  20.                         if($cAnswer==$index+1)
  21.                         {
  22.                               $correct_answer_class = 'correct-answer';
  23.                         }
  24.  
  25.                         $submitted_value = '';
  26.                        if($user_answers) 
  27.                        {
  28.                           if($uAnswer == $index+1) 
  29.                           {
  30.                                 $submitted_value = 'checked';
  31.  
  32.                             }
  33.                         }
  34.  
  35.                         ?> <li class="col-md-6 {{$correct_answer_class}} answer_radio" > <input type="radio" name="option{{$question->id.$outer_index}}" id="1radio1"  {{$submitted_value}} disabled=""> <label for="1radio1"> <span class="fa-stack radio-button"> <i class="mdi mdi-check active"></i> </span> {{ $value}}  </label> </li> <?php $index++; } ?> </ul> </div> </li> <hr> <?php } ?> </ul> </div> </div>
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.