Forum Moderators: coopster

Message Too Old, No Replies

Auto Populate Dropdown List (current year)

         

adammc

1:56 am on Feb 20, 2007 (gmt 0)

10+ Year Member



Hi guys,

How would I automate this script below so that I dont have to modify it each year (example; when the year reaches 2008)

[PHP]<?

// Make the years array.
$years = range (2007, 1930);

// Make the years pull-down menu.
foreach ($years as $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';

?>[/PHP]

Any help would be greatly appreciated :)

adammc

2:10 am on Feb 20, 2007 (gmt 0)

10+ Year Member



Will this do the trick ?

$currentYear = date("Y");

// Make the years array.
$years = range ($currentYear, 1930);

coopster

2:12 am on Feb 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you test it?

;) Yes, that is a great approach. Nice work.

adammc

2:33 am on Feb 20, 2007 (gmt 0)

10+ Year Member



Thanx coopster!

No I hadn't cause I wasnt sure how to test if its 2008 ;)

dreamcatcher

7:19 am on Feb 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or you could do it like this adammc:


for ($i=date("Y"); $i>1929; $i--)
{
echo "<option>$i</option>\n";
}

dc

adammc

8:39 am on Feb 20, 2007 (gmt 0)

10+ Year Member



HI Dreamcatcher :)

$i>1929; $i--)
If its not asking too much, can you please explain the code above?

dreamcatcher

9:18 am on Feb 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure thing adammc. :)

Ok, lets take a standard for loop. I assume you are familiar with for loops? If not..


for ($i=0; $i<5; $i++)
{
echo $i;
}

This sets the start count at 0 and increments $i until 4 is reached, then the for loop stops execution. The ++ is incrementing the count. $i is stating that the loop will run unless $i is less than 5. So, this loop runs from 0 - 4. Hope thats ok so far.

Now lets look at the code I posted:


for ($i=date("Y"); $i>1929; $i--)
{
echo "<option>$i</option>\n";
}

A little different this time. We set the initial count to date("Y") which will always be the current year. $-- increments the count backwards. So, in this instance we are incrementing $i backwards from the current year until 1930.

Did that make any sense at all?

dc

coopster

8:50 pm on Feb 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe it's just the old perl hacker in me, but you could actually get it all in one line without a loop ;)

print '<option>' . implode [php.net]("</option>\n<option>", range [php.net](date [php.net]('Y'), 1930)) . '</option>';

I use the implode() trick quite often when building lists and tables. Works slick and reduces code.

adammc

10:28 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



Fantastic explanation dreamcatcher, thank you :)

dreamcatcher

8:15 am on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use the implode() trick quite often when building lists and tables. Works slick and reduces code.

Coop, thanks for that, an interesting alternative. :)