For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > Populating "Month" Dropdown Menu









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Populating "Month" Dropdown Menu
cover

2006-01-19, 6:58 pm

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
Richard Conway

2006-01-19, 6:58 pm

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.
Justin Koivisto

2006-01-19, 6:58 pm

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_mon
th=-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

2006-01-19, 6:58 pm

Justin Koivisto wrote:

> cover wrote:
>
> /**
> * generates an HTML select element for the month
> *


Nice code.

C.
Justin Koivisto

2006-01-19, 6:58 pm

Colin McKinnon wrote:
> Justin Koivisto wrote:
>
>
> 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
Janwillem Borleffs

2006-01-22, 7:02 pm

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


finalwebsites.com

2006-01-23, 7:55 am

I have one where you can use labels in your own language:

http://www.finalwebsites.com/snippets.php?id=1

;-)

regards Olaf

Janwillem Borleffs

2006-01-24, 6:56 pm

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


Justin Koivisto

2006-01-24, 6:56 pm

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_mon
th=-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
Darkstar 3D

2006-01-24, 9:55 pm

And the race is on! :)

finalwebsites.com

2006-01-25, 3:57 am


Janwillem Borleffs wrote:
> finalwebsites.com wrote:
>
> Just prepend:
>
> setlocale(LC_TIME, 'nl_NL');
>
> or any other available locale...
>
>
> JW

You're right but this will not work on every server on the same way.

Olaf

Janwillem Borleffs

2006-01-25, 7:55 am

Justin Koivisto wrote:

> // this should never happen!
>


Definitely one of the most seen messages in log files ;-)


JW

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com