我想通过将DB值放入下拉列表来选择选项,以下是我使用过的代码,
$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>"; ?>
它工作正常,但我想每年只显示一个值,我想避免在同一年下降。请帮忙
答案 0 :(得分:1)
对apply_year和Create mysql查询使用DISTINCT,这样它只会在结果中返回apply_year一次:
$query = "SELECT DISTINCT apply_year FROM emp_leaves ORDER BY apply_year DESC";
答案 1 :(得分:1)
试试这个,
$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>"; ?>