简体   繁体   English

如何在多选表单上从数据库中回显多选中的选定值

[英]How to echo selected values on multiselect from database on a multiselect form

i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection 我有一个使用multiselect的注册表单,该表单根据选择将选项插入值1,2,3,4,5,6,7

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........ 我遇到了不明偏移量3,不明偏移量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>

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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM