How to delete duplicate value from drop down list

Related searches

I have the following coding with the values in drop down list and I would like to delete the duplicated value from the list but I don't know how to do??

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

Existing result in $_POST['dept']

AC HR AC Admin MIS MIS

Expecting result in $_POST['dept']

AC Admin HR MIS

I have modified your script

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

you can also fetch unique records in your query as you didn't posted query and updatiing your provided code.

[Solved] Remove duplicate values in DropDownList, you can use jquery to remove duplicate value temporary while its showing to user take all the values of dropdown in an array and match the� And now if you type a duplicate value, a warning dialog will pop out. Auto remove duplicates from a list by Kutools for Excel If you are not familiar with formulas, you can apply Kutools for Excel ’s Prevent Duplicate utility to stop from duplicates being typed in range at once.

Use DISTINCT on your SQL statement to get the unique result.

Example:

   SELECT DISTINCT col FROM Table

You can read the details about SQL DISTINCT here : https://www.w3resource.com/sql/select-statement/queries-with-distinct-multiple-columns.php

How do I remove duplicates from a drop-down list?, Hi, When user selects a row in gridview i need to display the selected value in the dropdownlist.. The dropdownlits are populated at page load� I have the following coding with the values in drop down list and I would like to delete the duplicated value from the list but I don't know how to do??

Since it's in an array you can use

$array = array_unique($array);

this will automatically detect any duplicates and remove them leaving only the first instance in the array, see https://www.php.net/manual/en/function.array-unique.php

EDIT: I'm not 100% sure if I understand how your data is coming in, but if I am understanding it correctly you can try this:

<select id="dept" name="dept" class="dept" width="100" style="width: 100px">
<?php
$fullNames = array();
while ($line = odbc_fetch_array($result)){
$fullNames=substr($line['fullName'],strpos($line['fullName'],'-')+1);
}
    $fullNames = array_unique($fullNames);
    if ($fullNames==$_POST['dept']){
        $selected="selected=\"selected\"";
    } else {
        $selected="";
}
echo "<option value=\"".$fullNames."\" $selected>".$fullNames."</option>";
}
?>
</select>

That said you probably don't want to be directly using $_POST['dept'] but rather will want to do some validation first on $_POST['dept'] then once you confirm it's a valid value then use it in a variable like this:

$dept = $_POST['dept'];

Then you would need to update the other code to use $dept rather than $_POST['dept']. But this will prevent any unintended consequences from a user submitting an invalid or worse yet a intentionally malformed dept. It is good practice to never trust or directly use user input but always test and validate it.

Remove duplicate values from dropdownlist ?, But make sure, not to do the refresh section as it will remove the already selected value from DropDown. Posted:� Repeat this process for any other list controls you need to remove duplicates from if necessary. At this point this view can now be dragged onto any form where you'll have views containing list controls that may have duplicate values in them and you can call them easily to remove them.

Maybe you can push names into an array with the current loop then filter out the duplicated values. Then use another loop to echo the tag.

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

How to avoid duplicate selections from a dropdown list , Create drop down list without duplicates by using Remove Duplicates. Create drop 6. Click OK, then the drop down list with unique values has been created. Select the range of cells that has duplicate values you want to remove. Tip: Remove any outlines or subtotals from your data before trying to remove duplicates. Click Data > Remove Duplicates, and then Under Columns, check or uncheck the columns where you want to remove the duplicates.

How to create a drop-down list from a table without duplicate in Excel?, Create drop down list without duplicates by using PivotTable. 1. Select the table, click Insert > PivotTable , then choose a position for the new PivotTable. 2. Click OK , then in the PivotTable Fields pane, drag the column you want to create drop down list based on to the Rows section. 3. Now the

Create a dictionary, using the List items as keys. This will automatically remove any duplicates because dictionaries cannot have duplicate keys. Create a Dictionary

Could you please assist me with removing duplicates in a drop down list.The list is not static. Example: Before. James Peter James Nick Peter After. James Peter Nick I am not sure what formula to use data validation formula box.

Comments
  • Actually, the data in the table as followings, so cannot use DISTINCT function....such as record 1 Peter Ma-AC, record 2 May Wong-AC, record 3 John Chen-HR, record 4 Mary Wong etc... –