0
votes

i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection

However, after inserting i want to provide an option for editing where users edit

and i want the already selected values from database to be preselected while looping other options as unselected

I am experiencing unidentified offset 3, unidentified offset 3........

<div class="form-group">
              <label for="touch-spin-1">Course Days</label>
             <select multiple="multiple" name="coursedays[]" style="width: 100%;" data-toggle="select2" data-placeholder="<?php //echo $course_days; ?>" data-allow-clear="true" >
                 <?php                   
                                                                             $entities  = "1,2,3,4,5,6,7";                                             $entity = explode(",",$entities);                                           $servs = explode("," , $course_days);                                                        $arr_length = count($entity);                                                      for($i=0;$i<=$arr_length;$i++){ 
              ?>
                <option <?php if (in_array($servs, $entity)) { echo "selected";} ?>  value="<?php echo $servs[$i]; ?>"> <?php echo $servs[$i]; ?></option>
                   <?php } ?></select></div>
1
'not working' isn't much help. Anything in error_log? What's the expected versus actual output? - delboy1978uk
Thanks for your interest, the error is ........ unidentified offset 3, unidentified offset 4........ - Ademaintain
Ok, so the array $servs does not have a key with array index 3, 4... var_dump($servs) and see what it DOES give you - delboy1978uk

1 Answers

0
votes

try in this way

<?php
            $course_days = "2,3,4";
            $entities = "1,2,3,4,5,6,7";

            $entity = explode(",", $entities);
            $servs = explode(",", $course_days);

            foreach($entity as $en) {
                ?>
            <option value="<?php echo $en; ?>" <?php echo (in_array($en, $servs)) ? 'selected' : ''; ?>>
                <?php echo $en; ?>
            </option>
            <?php } ?>