代码之家  ›  专栏  ›  技术社区  ›  Lakito

如何在PHP上向日期(年份)下拉菜单添加单词?

  •  -1
  • Lakito  · 技术社区  · 6 年前

    我在PHP网站上有一个下拉菜单,从2014年开始,到今年(2018年)结束。

    Drop down menu sample

    我想在这个菜单中加上“活动”这个词,它将包含所有年份。打开页面时,默认情况下会选择此选项。这是我多年来一直使用的代码。

      <?
    // Sets the top option to be the current year.
    $currently_selected = date('Y');
    // Year to start available options at
    $earliest_year = 2014;
    // Set your latest year you want in the range, in this case, current year
    $latest_year = date('Y');
    
    
    echo "Select year to display: ";
    print '<select>';
    
    //Loops over each int[year] from current year, back to $earliest_year [2014]
    foreach ( range( $latest_year, $earliest_year ) as $i ) {
        // Prints the option with the next year in range
        print '<option value="'.$i.'"'.($i === $currently_selected ? ' selected="selected"' : '').'>'.$i.'</option>';
        }
        print '</select>';
    
    
        ?>
    

    如何添加“活动”选项?

    编辑**

    这就是我现在的情况:

    <form method="get" action="<?=$this_page?>">
    
    <?
    
    
    
    // Sets the top option to be the current year.
    $currently_selected = date('Y');
    // Year to start available options at
    $earliest_year = 2014;
    // Set your latest year you want in the range, in this case, current year
    $latest_year = date('Y');
    
    
    echo "Select year to display: ";
    print "<select name=\"yearchosen\">";
    
    if(isset($_GET[submitok]))
    
    {
    
    ?>
    <option value="<?=$yearchosen?>" selected></option>
    <?} else {
    
    ?>
    
    
    <option value="Active" selected>Active</option>
    
    <?}
    
    //Loops over each int[year] from current year, back to $earliest_year [2014]
    foreach ( range( $latest_year, $earliest_year ) as $i ) {
        // Prints the option with the next year in range
        print '<option value="'.$i.'" >'.$i.'</option>';
        }
        print '</select>';
    
        ?>
    
            <td>
            <input style="padding-left:5px" name="submitok" type="submit" value="Update"/>
            </td>
    
    </form>
    

    --当我点击更新时,它会刷新站点,我会回显所选的值,但它不会显示在下拉菜单上。。。。。它再次显示ACTIVE(活动)。。。。我在这里错过了什么??

    3 回复  |  直到 6 年前
        1
  •  0
  •   J. Doe    6 年前

    如果这是在html页面中,我建议对foreach循环使用备用sytax,并将html部分保留为html(无打印或回送)

    您只需添加

    <option value="ACTIVE" selected="selected">ACTIVE</option>
    

    foreach循环外部

    您可以在foreach循环中更改现有选项,如下所示:

    <option value="'.$i.'".'>'.$i.'</option>
    
        2
  •  0
  •   ypoulakas    6 年前

    因此,如果您不希望选择当前年份,并且希望将通用选项添加到列表中,则需要更改:

    print '<option value="'.$i.'"'.($i === $currently_selected ? ' selected="selected"' : '').'>'.$i.'</option>';
    

    未来

    print '<option value="'.$i.'">'.$i.'</option>';
    

    然后在for循环后添加一个仅用于active的选项:

    print '<option value="active" selected="selected">Active</option>';
    

    但您的value=“active”需要替换为一个实际会吐回所有年份数据的值

        3
  •  0
  •   Osama Essam    6 年前

    因为您将整数与字符串匹配,所以只需将 + 之前 $currently_selected 将其转换为整数

    print '<option value="'.$i.'"'.($i === +$currently_selected ? ' selected="selected"' : '').'>'.$i.'</option>';