
November 7th, 2003, 03:48 PM
|
 |
Contributing User
|
|
Join Date: May 2003
Location: Tennessee
Posts: 1,355
Time spent in forums: < 1 sec
Reputation Power: 7
|
|
PHP Code:
<select name="strYear">
<?php
//Here I'm using the current date rather than hard-coding it as you had.
//Reduces maintenance issues down the road b/c you won't have to remember to edit the year each new year.
for($i=1980; $i< date('Y'); $i++){
//Ternary operator here:
//Evaluates $i==$year
//If true, return part after ?
//Else return the part after the colon
//This is just a shorthand way of doing an if-else statement.
//Basically, if the values match, we're returning the string " selected" and inserting it into the option tag. Else we're inserting nothing.
$selected=(($i==$year)?" selected":"");
print "<option value=\"" . $i . "\"" . $selected . ">" . $i . "</option>\n";
}
?>
</select>
You should be able to use the code with text or numbers. If you're using text, you'd want to create an array of possible text values and pass that to the for statement like so:
PHP Code:
<?php
for($i=0; $i<sizeof($textarray); $i++){
$selected=(($textarray[$i]==$databasevalue)?" selected":"");
print "<option value=\"" . $textarray[$i] . "\"" . $selected . ">" . $textarray[$i] . "</option>\n";
}
?>
|