Re: Dynamic Year Drop Down
| From: | Albert Padley | Date: | Fri, 17 Nov 2006 07:10:01 +0000 |
| Subject: | Re: Dynamic Year Drop Down | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-244768@lists.php.net to get a copy of this message | ||
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