[SOLVED] How can I achieve years on drop down menu


This Content is from Stack Overflow. Question asked by Alvin

Hello everyone one I want to get this kind of drop down menu which has years need to be selected

For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022


If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:

<div class="season-select-wrapper">
    // Define variables for the select
    $currentYear = 2022;
    $startYear = 1990;
    <label for="season">Select a season</label>
    <select name="season" id="season">
        <?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
            <option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
        <?php } ?>

This would create HTML markup like this:

<div class="season-select-wrapper">
    <label for="season">Select a season</label>
    <select name="season" id="season">
        <option value="2021/2022">2021/2022</option>
        <option value="2020/2021">2020/2021</option>
        <option value="2019/2020">2019/2020</option>
        <option value="2018/2019">2018/2019</option>
        <option value="2017/2018">2017/2018</option>
        <option value="2016/2017">2016/2017</option>
        <option value="2015/2016">2015/2016</option>
        <option value="2014/2015">2014/2015</option>
        <option value="2013/2014">2013/2014</option>
        <option value="2012/2013">2012/2013</option>
        <option value="2011/2012">2011/2012</option>
        <option value="2010/2011">2010/2011</option>
        <option value="2009/2010">2009/2010</option>
        <option value="2008/2009">2008/2009</option>
        <option value="2007/2008">2007/2008</option>
        <option value="2006/2007">2006/2007</option>
        <option value="2005/2006">2005/2006</option>
        <option value="2004/2005">2004/2005</option>
        <option value="2003/2004">2003/2004</option>
        <option value="2002/2003">2002/2003</option>
        <option value="2001/2002">2001/2002</option>
        <option value="2000/2001">2000/2001</option>
        <option value="1999/2000">1999/2000</option>
        <option value="1998/1999">1998/1999</option>
        <option value="1997/1998">1997/1998</option>
        <option value="1996/1997">1996/1997</option>
        <option value="1995/1996">1995/1996</option>
        <option value="1994/1995">1994/1995</option>
        <option value="1993/1994">1993/1994</option>
        <option value="1992/1993">1992/1993</option>
        <option value="1991/1992">1991/1992</option>
        <option value="1990/1991">1990/1991</option>

This Question was asked in StackOverflow by Alvin and Answered by jjmss It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?