Bluehost.com Web Hosting $6.95

Populating "Month" Dropdown Menu

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("%...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-19-2006
cover
 
Posts: n/a
Default Populating "Month" Dropdown Menu

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
Reply With Quote
  #2 (permalink)  
Old 01-19-2006
Richard Conway
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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.
Reply With Quote
  #3 (permalink)  
Old 01-19-2006
Justin Koivisto
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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
Reply With Quote
  #4 (permalink)  
Old 01-19-2006
Colin McKinnon
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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.

C.
Reply With Quote
  #5 (permalink)  
Old 01-20-2006
Justin Koivisto
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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
Reply With Quote
  #6 (permalink)  
Old 01-22-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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


Reply With Quote
  #7 (permalink)  
Old 01-23-2006
finalwebsites.com
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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

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

;-)

regards Olaf

Reply With Quote
  #8 (permalink)  
Old 01-24-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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


Reply With Quote
  #9 (permalink)  
Old 01-24-2006
Justin Koivisto
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

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
Reply With Quote
  #10 (permalink)  
Old 01-25-2006
Darkstar 3D
 
Posts: n/a
Default Re: Populating "Month" Dropdown Menu

And the race is on! :)

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0