如何在具有多对多关系的复选框中显示所选数据

时间:2017-08-22 11:28:17

标签: php mysql codeigniter

我正在尝试根据user_id从tbl_user_group获取数据,以及用户所属的组数

当我在我的视图中使用这段代码时,我就去了

代码:

 <label for="field-1" class="col-sm-3 control-label">Selected Groups <span class="required">*</span></label>
      <div class="col-sm-9">         
         <?php $query = $this->db->select('*')->from('tbl_user_group')->where('user_id', $editUser->user_id)->get()->result(); ?>

          <?php foreach ($query as $value) : ?>
                <?php foreach ($getTblUserGroup as $group) : ?>
                      <div class="c-checkbox needsclick">
                          <?php if ($group->id == $value->gropup_id) : ?>
                          <label class="needsclick">
                          <input type="checkbox"  checked="<?php $group->id == $value->gropup_id ? 'check' : '' ?>" value="<?php $group->id ?>" name="gropup_id[]" class="needsclick">
                          <strong class="badge btn-primary"><?= $group->group_name ?></strong>  
                          </label>

                        <?php endif; ?>
                      </div>
                   <?php endforeach; ?>                                    
              <?php endforeach; ?>

              </div>

结果;

enter image description here

当我尝试这段代码时:

<div class="col-sm-9">         
    <?php $query = $this->db->select('*')->from('tbl_user_group')->where('user_id', $editUser->user_id)->get()->result(); ?>

          <?php foreach ($query as $value) : ?>
              <?php foreach ($getTblUserGroup as $group) : ?>
                  <div class="c-checkbox needsclick">
                       <?php if ($group->id == $value->gropup_id) : ?>
                          <label class="needsclick">
                          <input type="checkbox"  checked="check" value="<?php $group->id ?>" name="gropup_id[]" class="needsclick">
                          <strong class="badge btn-primary"><?= $group->group_name ?></strong>  
                          </label>
                          <?php else : ?>
                          <label class="needsclick">
                          <input type="checkbox" value="<?php $group->id ?>" name="gropup_id[]" class="needsclick">
                          <strong class="badge btn-primary"><?= $group->group_name ?></strong>  
                          </label>    
                     <?php endif; ?>
                 </div>
              <?php endforeach; ?>    
          <?php endforeach; ?>    
      </div>

结果就是这个 enter image description here

如何检查用户所属的组以及其他未选中的组,但在我的情况下,我只检查或重复未分配的组。

0 个答案:

没有答案