为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

梵蒂冈之花 2021-05-10 16:18:49
我使用以下代码对下拉列表中的值进行编码,我想从列表中删除重复的值,但我不知道该怎么办?<select id="dept" name="dept" class="dept" width="100" style="width: 100px"><?phpwhile ($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人力资源管理信息系统
查看完整描述

3 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

我已经修改了您的脚本


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

<?php

$dropdown = array();

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

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


    $selected="";

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

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

    }


    $dropdown[$fullNames] = "<option value=\"".$fullNames."\" $selected>".$fullNames."</option>";    

}

echo implode('',$dropdown);

?>

</select>

您也可以在查询中获取唯一记录,因为您没有发布查询并更新提供的代码。


查看完整回答
反对 回复 2021-05-28
?
繁花如伊

TA贡献2012条经验 获得超12个赞

也许您可以使用当前循环将名称推送到数组中,然后过滤掉重复的值。然后使用另一个循环回显标签。


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

<?php

$array = []; 

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

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

    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) {

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

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

    }

    else {

       $selected="";

    }

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

    }

?>

</select>


查看完整回答
反对 回复 2021-05-28
  • 3 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信