Reputation: 419
I want to make select option by getting DB values into a drop down, following is my code that I have used,
$query = "SELECT apply_year FROM emp_leaves ORDER BY apply_year DESC";
$result = mysql_query($query);
echo "<select name='apply_year' class=\"form-control\"><option>Select Year</option>";
while ($r = mysql_fetch_array($result)) {
echo "<option value=" . $r['leave_id'] . ">" . $r['apply_year'] . "</option>";
}
echo "</select>"; ?>
It work fine, But I want to show only one value of each years, I want to avoid repeat same year in drop down. Please help
Upvotes: 0
Views: 43
Reputation: 1045
Try this,
$query = "SELECT apply_year FROM emp_leaves GROUP BY apply_year ORDER BY apply_year DESC";
$result = mysql_query($query);
echo "<select name='apply_year' class=\"form-control\"><option>Select Year</option>";
while ($r = mysql_fetch_array($result)) {
echo "<option value=" . $r['leave_id'] . ">" . $r['apply_year'] . "</option>";
}
echo "</select>"; ?>
Upvotes: 1
Reputation: 8101
Use DISTINCT for apply_year and Create mysql query like, so that it will return apply_year only once in result:
$query = "SELECT DISTINCT apply_year FROM emp_leaves ORDER BY apply_year DESC";
Upvotes: 1