0

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

1
  • 1
    how can you get the leave_id value when you only have "apply_year" in your query? - I also agree with the use of "distinct" as @Dhara Parmar said but you might also try "GROUP BY apply_year" as well. Commented Jun 13, 2016 at 7:36

2 Answers 2

1

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";
Sign up to request clarification or add additional context in comments.

Comments

1

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>"; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.