Forum Moderators: coopster
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 :)
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
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.