can i loop the values that will display in the dropdown menu

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
softsolvers
Forum Commoner
Posts: 75
Joined: Fri Feb 13, 2004 4:26 am
Location: India

can i loop the values that will display in the dropdown menu

Post by softsolvers »

hello i want to use loop in the drop down menu,i.e. i will like to display the
list of years like 1900 to 2004 ,how can i save the size of the code by implementing the loop in this menu
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

like this:

Code: Select all

echo "<select name='some_name'>\n";
for ($i=1900; $i<=2004; $i++) {
    echo "<option value='$i'>$i</option>\n";
}
echo "</select>\n";
I hope that helps
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Or..

Code: Select all

foreach(range(1900, 2004) as $year){
    echo '<option value="'.$year.'">'.$year.'</option>';
}
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

nice way to use a foreach with the range function. I never though of that. cool.
Post Reply