I’m trying to populate a form SELECT with OPTIONS from a table in my database. It should display each year once.
eg. If the date fields in the database are 02-09-2010, 10-14-2010, 08-09-2011 :
The dropdown should show:
2010
2011
Instead, it shows:
2010
2010
2011
My code:
$result = mysql_query("SELECT date FROM user_history");
$yearoptions = "";
while($row = mysql_fetch_array($result)) {
$date = $row['date'];
$yeararray = explode("-", $date);
$year = array_unique($yeararray);
$yearoptions .= '<option value="' . $year[2] . '">'
. $year[2] . '</option>';
Can anyone see what I’m doing wrong and offer a suggestion?
1 Answer