This is a discussion on Populating "Month" Dropdown Menu within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I can get the current month to display in a text box by inserting value="<? print strftime("%...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I can get the current month to display in a text box by inserting
value="<? print strftime("%B"); ?>"> into the code for that particular text box on a form but how can I populate a drop down with names of all the months with the current one being 'default' as it would be in the text box example? TIA |
|
|||
|
cover wrote:
> I can get the current month to display in a text box by inserting > value="<? print strftime("%B"); ?>"> into the code for that > particular text box on a form but how can I populate a drop down with > names of all the months with the current one being 'default' as it > would be in the text box example? TIA You want the following: <select name="month"> <option <?php if (date("m")=="01") echo "selected" ?>value="01">Jan</option> <option <?php if (date("m")=="02") echo "selected" ?>value="02">Feb</option> ... and so on... </select> Other tidier ways to do it using for loops too if you think about it. |
|
|||
|
cover wrote:
> I can get the current month to display in a text box by inserting > value="<? print strftime("%B"); ?>"> into the code for that > particular text box on a form but how can I populate a drop down with > names of all the months with the current one being 'default' as it > would be in the text box example? TIA /** * generates an HTML select element for the month * * @param string $name The name of the HTML field * @param int $this_month=-1 The month number to have selected. If not supplied, current month is used. If 0, no month is selected. * @return string */ function get_html_select_month($name='',$this_month=-1){ if(empty($name)) $name='month'; if($this_month==-1) $this_month=date('n'); $months=range(1,12); $str='<select name="'.$name.'">'."\n"; foreach($months as $month){ $str.=' <option value="'.$month.'"'; if($month==$this_month) $str.=' selected="selected"'; $str.='>'.date('F',mktime(0,0,0,$month,1,2006)).'</option>'."\n"; } $str.='</select>'."\n"; return $str; } -- Justin Koivisto, ZCE - justin@koivi.com http://koivi.com |
|
|||
|
Colin McKinnon wrote:
> Justin Koivisto wrote: > >> cover wrote: >>> names of all the months with the current one being 'default' as it >>> would be in the text box example? TIA >> /** >> * generates an HTML select element for the month >> * > > Nice code. Thanks. One of these days I am going to search usenet for all the functions that I have posted so that I can find them when I want them. ;) -- Justin Koivisto, ZCE - justin@koivi.com http://koivi.com |
|
|||
|
cover wrote:
> I can get the current month to display in a text box by inserting > value="<? print strftime("%B"); ?>"> into the code for that > particular text box on a form but how can I populate a drop down with > names of all the months with the current one being 'default' as it > would be in the text box example? TIA > $current_month = strftime("%B"); print "<select>\n"; for ($i = 1; $i < 13 && $m = strftime('%B', mktime(0,0,0,$i)); $i++) { if ($m == $current_month) { print "<option selected='selected'>$m</option>\n"; } else { print "<option>$m</option>\n"; } } print "</select>"; JW |
|
|||
|
I have one where you can use labels in your own language:
http://www.finalwebsites.com/snippets.php?id=1 ;-) regards Olaf |
|
|||
|
finalwebsites.com wrote:
> I have one where you can use labels in your own language: > > http://www.finalwebsites.com/snippets.php?id=1 > > ;-) > Just prepend: setlocale(LC_TIME, 'nl_NL'); or any other available locale... JW |
|
|||
|
finalwebsites.com wrote:
> I have one where you can use labels in your own language: > > http://www.finalwebsites.com/snippets.php?id=1 > > ;-) OK, so I modified my version to use setlocale and strftime and now mine does the same. ;) <?php /** * Generates an HTML select element for the month. Uses strftime, so * obeys the current (or provided) locale. If no month is supplied, * current month is used. If 0 is passed, no month is selected. * * @param string $name The name of the HTML field * @param int $this_month=-1 The month number to have selected. * @param mixed $locale=0 The locale(s) to use. 2nd parameter to setlocale * @return string */ function get_html_select_month($name='',$this_month=-1,$locale=0){ if($locale!==0){ // get current locale setting $curr_locale=setlocale(LC_TIME,0); // set to the passed locale if(!setlocale(LC_TIME,$locale)){ // unable to use that locale on this system return FALSE; } } if(empty($name)) $name='month'; if($this_month==-1) $this_month=date('n'); $months=range(1,12); $str='<select name="'.$name.'">'."\n"; foreach($months as $month){ $str.=' <option value="'.$month.'"'; if($month==$this_month) $str.=' selected="selected"'; $str.='>'.strftime('%B',mktime(0,0,0,$month,1,2006 )).'</option>'."\n"; } $str.='</select>'."\n"; if($locale!==0){ setlocale(LC_TIME,0); } if(isset($curr_locale)){ // set the locale back. if(!setlocale(LC_TIME,$curr_locale)){ // this should never happen! return FALSE; } } return $str; } ?> -- Justin Koivisto, ZCE - justin@koivi.com http://koivi.com |