Jump to content

Drop down help needed


coupe-r

Recommended Posts

Hey Guys,

 

I want the drop down to select the current month.  Currently, it always selects December, the last month in the list.

 

$pay_month =  "<select name=month_box id=month_box>";

$pay_month .="<option value=1 if($month==01) echo selected='selected' >January</option>";

$pay_month .="<option value=2 if($month==02) echo selected='selected' >February</option>";

$pay_month .="<option value=3 if($month==03) echo selected='selected' >March</option>";

$pay_month .="<option value=4 if($month==04) echo selected='selected' >April</option>";

$pay_month .="<option value=5 if($month==05) echo selected='selected' >May</option>";

$pay_month .="<option value=6 if($month==06) echo selected='selected' >June</option>";

$pay_month .="<option value=7 if($month==07) echo selected='selected' >July</option>";

$pay_month .="<option value=8 if($month==08) echo selected='selected' >August</option>";

$pay_month .="<option value=9 if($month==09) echo selected='selected' >September</option>";

$pay_month .="<option value=10 if($month==10) echo selected='selected' >October</option>";

$pay_month .="<option value=11 if($month==11) echo selected='selected' >November</option>";

$pay_month .="<option value=12 if($month==12) echo selected='selected' >December</option>";

$pay_month .="</select>";

Link to comment
https://forums.phpfreaks.com/topic/190626-drop-down-help-needed/
Share on other sites

nothing wrong with Thorpe's answer -- but I couldn't resist refactoring your code.

 

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$pay_month =   "";
foreach ($months as $key => $name) {
    //array is zero based, so +1 the month number
    $key++;
    $selected = ($key == $month) ? ' selected' : '';
    $pay_month .= "$name\n";
}
$pay_month .= '';

 

As usual, I didn't test this  :shrug:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.