Reputation: 221
I want to eliminate all the duplicates in a select dropdown list created with PHP.
My PHP code that creates the dropdown list is the following:
public static function getDropdownlist($conn)
{
//Initialize dropdown list
//--------------------------------------------------
$ddl_query = "select * from MIS_EMPLOYEES";
$stmt_ddl = oci_parse($conn, $ddl_query);
oci_execute($stmt_ddl);
//A default value -- this will be the selected item in the dropdown ##
$prosopiko = JRequest::getVar('bodies', 0);
if ($prosopiko == 0)
$default = 0;
else
$default = $prosopiko;
//Initialize array to store dropdown options ##
$options = array();
// $options = array_unique();
$options[] = JHTML::_('select.option', '0', 'Επιλέξτε');
while (($row = oci_fetch_array($stmt_ddl, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
$options[] = JHTML::_('select.option', $row['ID'], $row['POSITION']);
}
//Create <select name="month" class="inputbox"></select> ##
$dropdown = JHTML::_('select.genericlist', $options, 'bodies', 'class="inputbox"', 'value', 'text', $default);
return $dropdown;
}
}
But it brings all the duplicates written from an Oracle table.
How can I eliminate the duplicates? I tried array_unique
but I failed.
Upvotes: 2
Views: 66
Reputation: 9316
In your SQL statement, simply change it to gather distinct elements you are interested in.
Since you are only using two values in the above code for the value and text, something like this should work:
SELECT ID, POSITION
FROM MIS_EMPLOYEES
GROUP BY ID, POSITION
Upvotes: 3
Reputation: 1889
Easiest option is to modify your query to SELECT DISTINCT ID, POSITION
or GROUP BY ID, POSITION
. Other than that you'll need to build up an array and use array_unique
on that.
Upvotes: 3