Hi,

I am using this code to display years from 1950 to the current year in my <select> drop down. I would like to know how can I modify this code to show the dates in desc order so current year will start first in the drop down?

<?php
for ($i = 1950 ; $i <= date('Y'); $i++)
{ echo "<option>$i</option>"; }
?>

Thanks,
Jassim

    Can you, in plain english, explain what the above code does, step by step?
    1. it…
    2. then…
    3. etc…

      it just populate years in a select control from 1950 to the current year and I want to list it from the current year to year 1950

        So you want it to start with the current year, stop in 1950, and count downwards.

        What it does now is ....... (once you are capable of answering this question you'll know how to change it).

          sorry I will explain again. Look at the attached image.

          [ATTACH]4971[/ATTACH]

          you'll see the top of the starting from 1950 so user needs to scroll down to get the current year.

          what I want is the reverse.. so the current year will be at the top of the list

          selectdropdown.png
            jrahma;11034723 wrote:

            sorry I will explain again...

            you'll see the top of the starting from 1950 so user needs to scroll down to get the current year.

            what I want is the reverse.. so the current year will be at the top of the list

            We understand the question. Weedpacket is trying to help you understand the solution.

            What your loop does now:

            1. Start in 1950
            2. Stop in the current year
            3. Each turn, add 1.

            These three instructions map directly to the three statements in the [font=monospace]for[/font] loop. Observe:

            for( {start with 1950}; {stop in {current year}}; {each turn, add 1} ){}
            for(         $i = 1950;        $i <= date( 'Y' );              $i++  ){}
            

            So, describe what you want you loop to do in those same terms...

            1. Start in the current year
            2. Stop in 1950
            3. Each turn, subtract 1.

            ...and give the code a try.

              jrahma;11034723 wrote:

              sorry I will explain again.

              You do not need to explain again ... we all know what to do with your code. What is happening is that we are trying to get YOU to think about what to do with your code.

              Another hint: You need to modify/rearrange THIS line:

              for ($i = 1950 ; $i <= date('Y'); $i++)

                solved with this:

                for ($i = date('Y'); $i >= 1950; $i--)
                { echo "<option>$i</option>"; }
                

                Thanks anyhow.

                  traq;11034729 wrote:

                  So, describe what you want you loop to do in those same terms...

                  1. Start in the current year
                  2. Stop in 1950
                  3. Each turn, subtract 1.

                  ...and give the code a try.

                  jrahma;11034733 wrote:

                  solved with this:

                  for ($i = date('Y'); $i >= 1950; $i--)
                  { echo "<option>$i</option>"; }
                  

                  Thanks anyhow.

                  <?php]
                  for( {start in {current year}}; {stop in 1950}; {each turn, subtract 1}){ echo "<option>$i</option>"; }
                  for (         $i = date( 'Y' );     $i >= 1950;                   $i-- ){ echo "<option>$i</option>"; }
                  

                  perfect. 🙂

                    4 days later
                    jrahma;11034733 wrote:

                    solved with this:

                    for ($i = date('Y'); $i >= 1950; $i--)
                    { echo "<option>$i</option>"; }
                    

                    Thanks anyhow.

                    Yay!

                    Weedpacket wrote:

                    Give a man a fish ...

                      Just to be different:

                      foreach(range(date('Y'), 1950) as $year) {
                          echo "<option>$year</option>\n";
                      }
                      

                        Wow! ... I didn't realize range() would run with a negative step without you telling it to.

                        Although I suppose it does make sense ...

                          dalecosp;11034947 wrote:

                          Wow! ... I didn't realize range() would run with a negative step without you telling it to.

                          Although I suppose it does make sense ...

                          I wasn't positive until I tried it. 🙂

                            Write a Reply...