Re: Dynamic Year Drop Down

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks, Larry. This was close, but didn't quite work. I played around with the syntax and the following worked great.

$this_year = date('Y');
echo "<select>\n";
for ($i= $this_year-1; $i < $this_year+3; ++$i) {
	echo "<option value='$i'";
	if($i == $this_year)
		echo "selected = 'selected'";
	echo ">" . $i . "</option>\n";
}	
echo "</select>\n";

Al Padley

On Nov 16, 2006, at 11:32 PM, Larry Garfield wrote:

Quite simple:

$this_year = date('Y');
for ($i= $this_year-1; $i < $this_year+3; ++$i) {
  print "<option value='$i'" .
($i==$this_year ? "selected='selected' : '') . ">$i</option>\n";
}

Obviously modify for however you're doing output. (Note that you DO want to have the redundant value attribute in there, otherwise some Javascript things don't work right in IE. Good habit to get into.) I don't think it can
really get more simple and elegant.

On Thursday 16 November 2006 23:26, Albert Padley wrote:
I want to build a select drop down that includes last year, the
current year and 3 years into the future. Obviously, I could easily
hard code this or use a combination of the date and mktime functions
to populate the select. However, I'm looking for a more elegant way
of doing this.

Thanks for pointing me in the right direction.

Al Padley

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux