在PHP中编辑时,如何在不重复的情况下显示多选中的选定值


How to show selected value in multiple select without repitition while editing in PHP

Im对新条目使用相同的表单,并对培训详细信息进行编辑。在那里,我有一份实习生名单,它被保存在单独的表格中。因此,在编辑特定条目时,我必须在表单中显示所选条目的数据。为此,我使用$_GET['act'] =="edit"来显示数据库中的值或新条目的空控件。在这种情况下,我有选择控制,在那里我可以为培训课程选择多个受训者。因此,在编辑时,我会阅读受训人员数据库表,并显示选定的受训人员。如果我总共有10名员工,现在我从选项中选择了5名员工。我必须将这5名员工显示为已选中,另5名员工则显示为未选中。但当我使用以下代码执行此操作时,我的"选择"控件会有"5名员工被选中"answers"所有10名员工都未被选中"等选项。所以总共有15个选项。但我只想有10种选择。

<?php
try {
   $sql = "SELECT * FROM enrollment WHERE enrollid = :cid";
   $stmt = $DB->prepare($sql);
   $stmt->bindValue(":cid",intval($_GET['cid']));   
   $stmt->execute();
   $enrollresults = $stmt->fetchAll();
} catch (Exception $ex) {
  echo $ex->getMessage();
}
}
try {
 $sql = "SELECT * FROM traineelist WHERE trainingid = :trid";
 $stmt = $DB->prepare($sql);
 $stmt->bindValue(":trid",$enrollresults[0]["enrollid"]);  
 $stmt->execute();
 $trlistresult = $stmt->fetchAll();
} 
catch (Exception $ex) 
{  echo $ex->getMessage(); }
try {
 $sql = "SELECT * FROM employees";
 $stmt = $EMPDB->prepare($sql);                
 $stmt->execute();
 $empresult = $stmt->fetchAll();
} 
catch (Exception $ex) 
{  echo $ex->getMessage(); }    
?>
    <div class="form-group">
            <label for="tr" class="control-label col-md-2">Trainee</label>      
            <div class="col-md-4"> 
                  <select id="trainee" name="tr[]"  multiple="multiple">              
                 <?php           
                 if(isset($_GET['act']) && $_GET['act']=="edit")
                  { ?>
                      <?php foreach($trlistresult as $row1){ ?>
                      <option value="<?php echo $row1["trainee"]?>" selected="selected"><?php echo $row1["trainee"]?></option>
                      <?php  } ?>
                     <?php foreach($empresult as $row){ ?>
                    <option><?php echo $row['first_name']?></option>
                  <?php  } ?>
                <?php
                  }          
                  else
                  {
                     foreach($empresult as $row){ ?>
                    <option><?php echo $row['first_name']?></option>
                    <?php } }?>   
                </select>       
            </div>
        </div>
I want like, it shows the default options with the selected value.

例如:Status:Active,InActive-如果我在from中选择"Active",而编辑"Status"控件时,会显示Active(selected)、Active、InActive。如何避免这种重复?

您可以准备$row1["trainee"]的值数组。然后更换您的代码

<?php           
if(isset($_GET['act']) && $_GET['act']=="edit")
{ ?>
   <?php foreach($trlistresult as $row1){ ?>
   <option value="<?php echo $row1["trainee"]?>" selected="selected"><?php echo $row1["trainee"]?></option>
   <?php  } ?>
   <?php foreach($empresult as $row){ ?>
   <option><?php echo $row['first_name']?></option>
   <?php  }
} ?>

带有:

if(isset($_GET['act']) && $_GET['act']=="edit")
{
    foreach($empresult as $row){ 
        if(in_array($row['first_name'], $trlistResultArray)) $selected = "selected";
        else  $selected = "";
        ?>
        <option <?php echo $selected; ?> ><?php echo $row['first_name']?></option>
        <?php
    }
}

如果你来自$empresult的名字在受训者的数组中,则将其设置为选中。

跳起来有帮助!