0

I Have issue in coding, I got Error like Undefined offset: 3. I am trying to auto checked checkbox. Please help for solve this issue.

My code

                               if($sites)
                               {
                                 $sites2 =explode (',',$sites);
                               }
                               else
                               {
                                 $sites2 ="";
                               }

                                //print_r ($sites2); 


                                $usersites = DB::table('sites')->get();   

                                $i=0;   

                                foreach($usersites as $row)
                                {

                                  $s_id = $row->s_id;
                            ?>
                            <div class="checkbox checkbox-success checkbox-inline btn btn-default col-sm-10 col-xs-10">
                            <input name="sites[]" type="checkbox" class="styled" id="inlineCheckbox6<?= $i; ?>" value="<?= $s_id; ?>" <?php if($sites2[$i] == $s_id) { echo "checked"; }?>>
                                <label for="inlineCheckbox6<?= $i; ?>" style="padding-left:0px;"><?= ucfirst($row->sname); ?> </label>
                            </div>
                            <?php

                            $i++;
                             }
                            ?>
solanki
  • 208
  • 1
  • 4
  • 18

1 Answers1

0

The error "Undefined Offset" means you are trying to access an index of an array that does not exist.

My best guess is that the offending code is:

if($sites2[$i] == $s_id){
    echo "checked";
}

And an Undefined offset of 3 is telling you that $sites2[3] does not exist. Instead, you could try something like:

if(isset($sites2[$i]) && $sites2[$i] == $s_id){
    echo "checked";
}
Daric
  • 440
  • 1
  • 4
  • 12