如何为此下拉菜单创建循环?

时间:2011-07-19 21:00:28

标签: php html forms

而且,循环会更有效率,然后逐个列出它们吗?

<select name="birthdayYear" >
    <option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
    <option value="2011"<?php echo $birthdayYear == '2011' ? 'selected="selected"' : ''; ?>>2011</option>
    <option value="2010"<?php echo $birthdayYear == '2010' ? 'selected="selected"' : ''; ?>>2010</option>
    <option value="2009"<?php echo $birthdayYear == '2009' ? 'selected="selected"' : ''; ?>>2009</option>
    <option value="2008"<?php echo $birthdayYear == '2008' ? 'selected="selected"' : ''; ?>>2008</option>
    <option value="2007"<?php echo $birthdayYear == '2007' ? 'selected="selected"' : ''; ?>>2007</option>
    <option value="2006"<?php echo $birthdayYear == '2006' ? 'selected="selected"' : ''; ?>>2006</option>
    <option value="2005"<?php echo $birthdayYear == '2005' ? 'selected="selected"' : ''; ?>>2005</option>
    <option value="2004"<?php echo $birthdayYear == '2004' ? 'selected="selected"' : ''; ?>>2004</option>
    <option value="2003"<?php echo $birthdayYear == '2003' ? 'selected="selected"' : ''; ?>>2003</option>
    <option value="2002"<?php echo $birthdayYear == '2002' ? 'selected="selected"' : ''; ?>>2002</option>
    <option value="2001"<?php echo $birthdayYear == '2001' ? 'selected="selected"' : ''; ?>>2001</option>
    <option value="2000"<?php echo $birthdayYear == '2000' ? 'selected="selected"' : ''; ?>>2000</option>
    <option value="1999"<?php echo $birthdayYear == '1999' ? 'selected="selected"' : ''; ?>>1999</option>
    <option value="1998"<?php echo $birthdayYear == '1998' ? 'selected="selected"' : ''; ?>>1998</option>
    <option value="1997"<?php echo $birthdayYear == '1997' ? 'selected="selected"' : ''; ?>>1997</option>
    <option value="1996"<?php echo $birthdayYear == '1996' ? 'selected="selected"' : ''; ?>>1996</option>
    <option value="1995"<?php echo $birthdayYear == '1995' ? 'selected="selected"' : ''; ?>>1995</option>
    <option value="1994"<?php echo $birthdayYear == '1994' ? 'selected="selected"' : ''; ?>>1994</option>
    <option value="1993"<?php echo $birthdayYear == '1993' ? 'selected="selected"' : ''; ?>>1993</option>
    <option value="1992"<?php echo $birthdayYear == '1992' ? 'selected="selected"' : ''; ?>>1992</option>
    <option value="1991"<?php echo $birthdayYear == '1991' ? 'selected="selected"' : ''; ?>>1991</option>
    <option value="1990"<?php echo $birthdayYear == '1990' ? 'selected="selected"' : ''; ?>>1990</option>
    <option value="1989"<?php echo $birthdayYear == '1989' ? 'selected="selected"' : ''; ?>>1989</option>
    <option value="1988"<?php echo $birthdayYear == '1988' ? 'selected="selected"' : ''; ?>>1988</option>
    <option value="1987"<?php echo $birthdayYear == '1987' ? 'selected="selected"' : ''; ?>>1987</option>
    <option value="1986"<?php echo $birthdayYear == '1986' ? 'selected="selected"' : ''; ?>>1986</option>
    <option value="1985"<?php echo $birthdayYear == '1985' ? 'selected="selected"' : ''; ?>>1985</option>
    <option value="1984"<?php echo $birthdayYear == '1984' ? 'selected="selected"' : ''; ?>>1984</option>
    <option value="1983"<?php echo $birthdayYear == '1983' ? 'selected="selected"' : ''; ?>>1983</option>
    <option value="1982"<?php echo $birthdayYear == '1982' ? 'selected="selected"' : ''; ?>>1982</option>
    <option value="1981"<?php echo $birthdayYear == '1981' ? 'selected="selected"' : ''; ?>>1981</option>
    <option value="1980"<?php echo $birthdayYear == '1980' ? 'selected="selected"' : ''; ?>>1980</option>
    <option value="1979"<?php echo $birthdayYear == '1979' ? 'selected="selected"' : ''; ?>>1979</option>
    <option value="1978"<?php echo $birthdayYear == '1978' ? 'selected="selected"' : ''; ?>>1978</option>
    <option value="1977"<?php echo $birthdayYear == '1977' ? 'selected="selected"' : ''; ?>>1977</option>
    <option value="1976"<?php echo $birthdayYear == '1976' ? 'selected="selected"' : ''; ?>>1976</option>
    <option value="1975"<?php echo $birthdayYear == '1975' ? 'selected="selected"' : ''; ?>>1975</option>
    <option value="1974"<?php echo $birthdayYear == '1974' ? 'selected="selected"' : ''; ?>>1974</option>
    <option value="1973"<?php echo $birthdayYear == '1973' ? 'selected="selected"' : ''; ?>>1973</option>
    <option value="1972"<?php echo $birthdayYear == '1972' ? 'selected="selected"' : ''; ?>>1972</option>
    <option value="1971"<?php echo $birthdayYear == '1971' ? 'selected="selected"' : ''; ?>>1971</option>
    <option value="1970"<?php echo $birthdayYear == '1970' ? 'selected="selected"' : ''; ?>>1970</option>
    <option value="1969"<?php echo $birthdayYear == '1969' ? 'selected="selected"' : ''; ?>>1969</option>
    <option value="1968"<?php echo $birthdayYear == '1968' ? 'selected="selected"' : ''; ?>>1968</option>
    <option value="1967"<?php echo $birthdayYear == '1967' ? 'selected="selected"' : ''; ?>>1967</option>
    <option value="1966"<?php echo $birthdayYear == '1966' ? 'selected="selected"' : ''; ?>>1966</option>
    <option value="1965"<?php echo $birthdayYear == '1965' ? 'selected="selected"' : ''; ?>>1965</option>
    <option value="1964"<?php echo $birthdayYear == '1964' ? 'selected="selected"' : ''; ?>>1964</option>
    <option value="1963"<?php echo $birthdayYear == '1963' ? 'selected="selected"' : ''; ?>>1963</option>
    <option value="1962"<?php echo $birthdayYear == '1962' ? 'selected="selected"' : ''; ?>>1962</option>
    <option value="1961"<?php echo $birthdayYear == '1961' ? 'selected="selected"' : ''; ?>>1961</option>
    <option value="1960"<?php echo $birthdayYear == '1960' ? 'selected="selected"' : ''; ?>>1960</option>
    <option value="1959"<?php echo $birthdayYear == '1959' ? 'selected="selected"' : ''; ?>>1959</option>
    <option value="1958"<?php echo $birthdayYear == '1958' ? 'selected="selected"' : ''; ?>>1958</option>
    <option value="1957"<?php echo $birthdayYear == '1957' ? 'selected="selected"' : ''; ?>>1957</option>
    <option value="1956"<?php echo $birthdayYear == '1956' ? 'selected="selected"' : ''; ?>>1956</option>
    <option value="1955"<?php echo $birthdayYear == '1955' ? 'selected="selected"' : ''; ?>>1955</option>
    <option value="1954"<?php echo $birthdayYear == '1954' ? 'selected="selected"' : ''; ?>>1954</option>
    <option value="1953"<?php echo $birthdayYear == '1953' ? 'selected="selected"' : ''; ?>>1953</option>
    <option value="1952"<?php echo $birthdayYear == '1952' ? 'selected="selected"' : ''; ?>>1952</option>
    <option value="1951"<?php echo $birthdayYear == '1951' ? 'selected="selected"' : ''; ?>>1951</option>
    <option value="1950"<?php echo $birthdayYear == '1950' ? 'selected="selected"' : ''; ?>>1950</option>
    <option value="1949"<?php echo $birthdayYear == '1949' ? 'selected="selected"' : ''; ?>>1949</option>
    <option value="1948"<?php echo $birthdayYear == '1948' ? 'selected="selected"' : ''; ?>>1948</option>
    <option value="1947"<?php echo $birthdayYear == '1947' ? 'selected="selected"' : ''; ?>>1947</option>
    <option value="1946"<?php echo $birthdayYear == '1946' ? 'selected="selected"' : ''; ?>>1946</option>
    <option value="1945"<?php echo $birthdayYear == '1945' ? 'selected="selected"' : ''; ?>>1945</option>
    <option value="1944"<?php echo $birthdayYear == '1944' ? 'selected="selected"' : ''; ?>>1944</option>
    <option value="1943"<?php echo $birthdayYear == '1943' ? 'selected="selected"' : ''; ?>>1943</option>
    <option value="1942"<?php echo $birthdayYear == '1942' ? 'selected="selected"' : ''; ?>>1942</option>
    <option value="1941"<?php echo $birthdayYear == '1941' ? 'selected="selected"' : ''; ?>>1941</option>
    <option value="1940"<?php echo $birthdayYear == '1940' ? 'selected="selected"' : ''; ?>>1940</option>
    <option value="1939"<?php echo $birthdayYear == '1939' ? 'selected="selected"' : ''; ?>>1939</option>
    <option value="1938"<?php echo $birthdayYear == '1938' ? 'selected="selected"' : ''; ?>>1938</option>
    <option value="1937"<?php echo $birthdayYear == '1937' ? 'selected="selected"' : ''; ?>>1937</option>
    <option value="1936"<?php echo $birthdayYear == '1936' ? 'selected="selected"' : ''; ?>>1936</option>
    <option value="1935"<?php echo $birthdayYear == '1935' ? 'selected="selected"' : ''; ?>>1935</option>
    <option value="1934"<?php echo $birthdayYear == '1934' ? 'selected="selected"' : ''; ?>>1934</option>
    <option value="1933"<?php echo $birthdayYear == '1933' ? 'selected="selected"' : ''; ?>>1933</option>
    <option value="1932"<?php echo $birthdayYear == '1932' ? 'selected="selected"' : ''; ?>>1932</option>
    <option value="1931"<?php echo $birthdayYear == '1931' ? 'selected="selected"' : ''; ?>>1931</option>
    <option value="1930"<?php echo $birthdayYear == '1930' ? 'selected="selected"' : ''; ?>>1930</option>
    <option value="1929"<?php echo $birthdayYear == '1929' ? 'selected="selected"' : ''; ?>>1929</option>
    <option value="1928"<?php echo $birthdayYear == '1928' ? 'selected="selected"' : ''; ?>>1928</option>
    <option value="1927"<?php echo $birthdayYear == '1927' ? 'selected="selected"' : ''; ?>>1927</option>
    <option value="1926"<?php echo $birthdayYear == '1926' ? 'selected="selected"' : ''; ?>>1926</option>
    <option value="1925"<?php echo $birthdayYear == '1925' ? 'selected="selected"' : ''; ?>>1925</option>
    <option value="1924"<?php echo $birthdayYear == '1924' ? 'selected="selected"' : ''; ?>>1924</option>
    <option value="1923"<?php echo $birthdayYear == '1923' ? 'selected="selected"' : ''; ?>>1923</option>
    <option value="1922"<?php echo $birthdayYear == '1922' ? 'selected="selected"' : ''; ?>>1922</option>
    <option value="1921"<?php echo $birthdayYear == '1921' ? 'selected="selected"' : ''; ?>>1921</option>
    <option value="1920"<?php echo $birthdayYear == '1920' ? 'selected="selected"' : ''; ?>>1920</option>
    <option value="1919"<?php echo $birthdayYear == '1919' ? 'selected="selected"' : ''; ?>>1919</option>
    <option value="1918"<?php echo $birthdayYear == '1918' ? 'selected="selected"' : ''; ?>>1918</option>
    <option value="1917"<?php echo $birthdayYear == '1917' ? 'selected="selected"' : ''; ?>>1917</option>
    <option value="1916"<?php echo $birthdayYear == '1916' ? 'selected="selected"' : ''; ?>>1916</option>
    <option value="1915"<?php echo $birthdayYear == '1915' ? 'selected="selected"' : ''; ?>>1915</option>
    <option value="1914"<?php echo $birthdayYear == '1914' ? 'selected="selected"' : ''; ?>>1914</option>
    <option value="1913"<?php echo $birthdayYear == '1913' ? 'selected="selected"' : ''; ?>>1913</option>
    <option value="1912"<?php echo $birthdayYear == '1912' ? 'selected="selected"' : ''; ?>>1912</option>
    <option value="1911"<?php echo $birthdayYear == '1911' ? 'selected="selected"' : ''; ?>>1911</option>
    <option value="1910"<?php echo $birthdayYear == '1910' ? 'selected="selected"' : ''; ?>>1910</option>
    <option value="1909"<?php echo $birthdayYear == '1909' ? 'selected="selected"' : ''; ?>>1909</option>
    <option value="1908"<?php echo $birthdayYear == '1908' ? 'selected="selected"' : ''; ?>>1908</option>
    <option value="1907"<?php echo $birthdayYear == '1907' ? 'selected="selected"' : ''; ?>>1907</option>
    <option value="1906"<?php echo $birthdayYear == '1906' ? 'selected="selected"' : ''; ?>>1906</option>
    <option value="1905"<?php echo $birthdayYear == '1905' ? 'selected="selected"' : ''; ?>>1905</option>
    <option value="1904"<?php echo $birthdayYear == '1904' ? 'selected="selected"' : ''; ?>>1904</option>
    <option value="1903"<?php echo $birthdayYear == '1903' ? 'selected="selected"' : ''; ?>>1903</option>
    <option value="1902"<?php echo $birthdayYear == '1902' ? 'selected="selected"' : ''; ?>>1902</option>
    <option value="1901"<?php echo $birthdayYear == '1901' ? 'selected="selected"' : ''; ?>>1901</option>
    <option value="1900"<?php echo $birthdayYear == '1900' ? 'selected="selected"' : ''; ?>>1900</option>
</select>

10 个答案:

答案 0 :(得分:2)

如果效率不高,它肯定更具可读性和可重用性,这一点更为重要。

<select name="birthdayYear" >
    <option value="0000">Year:</option>
    <?php
        for(var $x = year(); $x >= 1900; $x--) {
            var $selected = "";
            if($x == $birthdayYear) {
                $selected = " selected = 'selected'";
            }
            echo("<option value="$x"$selected>$x</option>");
        }
    ?>
</select>

简而言之,我的头发是一只鸟。

答案 1 :(得分:2)

基本上,你会:

  • 自己处理0000特定行,
  • 然后,从20111900
  • 进行for()循环

循环可能看起来像这样:

for ($year=2011 ; $year>=1900 ; $year--) {
    echo '<option value="' . $year . '"';
    if ($year == $birthdayYear) {
        echo 'selected="selected"';
    }
    echo '>' . $year . '</option>' . PHP_EOL;
}


当然,2011年可能不应该是硬编码的 - 请查看date()获取本年度的可能方法。

答案 2 :(得分:1)

for ($x=(int)date("Y"); $x>=1900; $x=$x--) {
    echo "<option value=\"", $x, "\"", ($birthdayYear==$x) ? " selected=\"selected\"" : "", ">", $x, "</option>"
}

这有额外的好处,你不必每年更新它。

答案 3 :(得分:1)

首先,输出'0000'选项。它不遵循模式,因此它不应该包含在循环中:

<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>

接下来,创建一个循环并输出该选项的每个部分。我从date('Y')(当前年份)而不是硬编码的“2011”开始,因此您的脚本不需要每年更新。

for ( $i = date('Y'); $i >= 1900; $i-- ) {
    echo "<option value=\"$i\"";
    echo $birthdayYear == $i ? 'selected="selected"' : '';
    echo ">$i</option>";
}

三个echo语句可以组合成一个。为清楚起见,我将它们放在不同的行上。

答案 4 :(得分:1)

试试这个:

<select name="birthdayYear" >
<?php
if($year == '0000') {
    echo '<option value="0000" selected="selected">0000</option>';
} else {
    echo '<option value="0000">0000</option>';
}
for($year = intval(date('Y')); $year > 1900; $year --) {
    if($year == $birthdayYear) {
        echo '<option value="'.$year.'" selected="selected">'.$year.'</option>';
    } else {
        echo '<option value="'.$year.'">'.$year.'</option>';
    }
}
?>
</select>

答案 5 :(得分:1)

<select name="birthdayYear" >
    <option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>

<?php
for($i=date('Y'); $i>1899; $i--) {
    $selected = '';
    if ($birthdayYear == $i) $selected = ' selected="selected"';
    print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>

</select>
  

而且,循环会更有效率,然后逐个列出它们吗?

我认为你不必担心效率问题。使用循环看起来更干净,我认为更容易维护。

答案 6 :(得分:1)

1900至少和2011年一样随意。我们可以制作一个仅比当年长100年的产品。

<?php $selectedYear = $_REQUEST['birthdayYear'] ?>
<select id="birthdayYear">
<option value="0">Year:</option>
<?php $currYear = date('Y') ?>
<?php for ($i=0; $i<100; ++$i) : ?>
<?php $displayYear = $currYear-$i ?>
<option value="<?= $displayYear ?>"<?= ($displayYear == $selectedYear ? 'selected="selected"' : '') ?>><?= $displayYear ?></option>
<?php endfor; ?>
</select>

要准确回到1900,只需将for条件更改为112。

答案 7 :(得分:0)

循环,拜托。这不仅仅是代码效率的问题(在这个层面上,根本不重要),而是开发人员效率的一个问题:一个循环在眼睛上更容易,它不会强迫读者滚动或折叠,它减少了错字引入细微错误的可能性(有一个循环,它们都是错误的或者都是正确的。)

老实说,任何此类常规模式中包含两个以上条目的下拉列表都会让我对for关键字进行抽搐和触及。其中一百人接近尴尬。

答案 8 :(得分:0)

<select name="birthday">
<option value="0">-</option>
<?php
    for($i=1990; $i<=2013; $i++) { ?>

<option value="<?php echo $i; ?>">

<?php echo $i; ?>

答案 9 :(得分:-2)

就个人而言,我不会循环这个。我会将它放在一个单独的PHP文件中,并将其包含在您的页面中。这种方式不会占用所需的空间,您可以轻松地从一个文件修改数据,该文件将在您的应用程序中更新。