Hey guys, I wrote the following functions to give me 3 drop downs, one for month one for year and one for day. I made them so they can take an input if the form being printed is for editing a data set, to make the date from the database be the default selection for the drop downs. I was just wondering where it might be improved upon, it works exactly as I want it to, just not sure how/if it can be made better.
[code=php]
function monthSelect($fieldname,$selected = 0) {
$month = array(‘January’
,’February’
,’March’
,’April’
,’May’
,’June’
,’July’
,’August’
,’September’
,’October’
,’November’
,’December’
); //used to create month drop down list
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Month</option>n”;
for( $i=0;$i<=11;$i++) {
$value = $i+1;
//Use month array to create month options
if( $selected == $value ) {
// If the set month is the current option, make it selected
$select .= “<option value=”{$value}” selected>$month[$i]</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$value}”>{$month[$i]}</option>n”;
}
}
$select .= ‘</select>’; // close select
return $select;
}
function daySelect($fieldname,$selected = 0) {
// Prints 31 days
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Day</option>n”;
for( $i=1; $i<32; $i++ ) {
if( $selected == $i ) {
// If the set day is the one being printed, make it selected
$select .= “<option value=”{$i}” selected>{$i}</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$i}”>{$i}</option>n”;
}
}
$select .= “</select>”; // close the select
return $select;
}
function yearSelect($fieldname,$selected = 0) {
// Prints 25 years starting 1 year in the future
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Year</option>n”;
for( $i=-1; $i<25; $i++ ) {
$year = date(“Y”)-$i;
if( $year == $selected ) {
// If the set year is the current option, make it selected
$select .= “<option value=”{$year}” selected>{$year}</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$year}”>{$year}</option>n”;
}
}
$select .= “</select>”; // close select
return $select;
}