如何从下拉列表中删除重复值

我使用以下代码对下拉列表中的值进行编码,我想从列表中删除重复的值,但我不知道该怎么办?


<select id="dept" name="dept" class="dept" width="100" style="width: 100px">

<?php

while ($line = odbc_fetch_array($result)){

$fullNames=substr($line['fullName'],strpos($line['fullName'],'-')+1);

if ($fullNames==$_POST['dept']){

    $selected="selected=\"selected\"";

}

else {

        $selected="";

}

echo "<option value=\"".$fullNames."\" $selected>".$fullNames."</option>";

}

?>

</select>

$ _POST ['dept']中的现有结果


AC HR AC Admin MIS MIS


预期结果为$ _POST ['dept']


AC Admin人力资源管理信息系统


梵蒂冈之花
浏览 153回答 3
3回答

慕妹3242003

我已经修改了您的脚本<select id="dept" name="dept" class="dept" width="100" style="width: 100px"><?php$dropdown = array();while ($line = odbc_fetch_array($result)){&nbsp; &nbsp; $fullNames=substr($line['fullName'],strpos($line['fullName'],'-')+1);&nbsp; &nbsp; $selected="";&nbsp; &nbsp; if($fullNames==$_POST['dept'])&nbsp; &nbsp; &nbsp; &nbsp; $selected="selected=\"selected\"";&nbsp; &nbsp; }&nbsp; &nbsp; $dropdown[$fullNames] = "<option value=\"".$fullNames."\" $selected>".$fullNames."</option>";&nbsp; &nbsp;&nbsp;}echo implode('',$dropdown);?></select>您也可以在查询中获取唯一记录,因为您没有发布查询并更新提供的代码。

繁花如伊

也许您可以使用当前循环将名称推送到数组中,然后过滤掉重复的值。然后使用另一个循环回显标签。<select id="dept" name="dept" class="dept" width="100" style="width: 100px"><?php$array = [];&nbsp;while ($line = odbc_fetch_array($result)){&nbsp; &nbsp; $fullNames=substr($line['fullName'],strpos($line['fullName'],'-')+1);&nbsp; &nbsp; array_push($array,$fullNames);//push all names into a array}$array = array_unique($array); //filter the duplicate names.//another loop to echo the <option>foreach ($array as $fullNames) {&nbsp; &nbsp; if ($fullNames==$_POST['dept']){&nbsp; &nbsp; &nbsp; &nbsp;$selected="selected=\"selected\"";&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp;$selected="";&nbsp; &nbsp; }&nbsp; &nbsp; echo "<option value=\"".$fullNames."\" $selected>".$fullNames."</option>";&nbsp; &nbsp; }?></select>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript