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