Month Drop Down Menue Code ??? Setting Current Month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Month Drop Down Menue Code ??? Setting Current Month

Post by edawson003 »

I am using the below code to generate a drop down menu for month. How can I modify this so that the current month is the default selection?

Code: Select all

<?php
$options = array(0=>'00', 1=>'01',2=>'02',3=>'03',4=>'04',5=>'05',6=>'06',7=>'07',8=>'08',9=>'09',10=>'10',11=>'11',12=>'12'); 
$selectedValue = isset($_POST['month']) ? $_POST['month'] : '';
 echo '<select class=clp name="month">' . "\n";
 foreach($options as $key=>$value) {
     echo '<option value="' . $key. '" ' . ($key== $selectedValue ? 'selected="selected"' : '') . '>' . $value . '</option>' . "\n";
 }
 echo '</select>';
 ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Month Drop Down Menue Code ??? Setting Current Month

Post by requinix »

Code: Select all

$selectedValue = isset($_POST['month']) ? $_POST['month'] : '';
How about setting the $selectedValue to be the current month? In fact, you could probably set $_POST["month"] to be date("n").
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Month Drop Down Menue Code ??? Setting Current Month

Post by edawson003 »

K. Thanks. I went with setting,

Code: Select all

$_POST['month'] = date(m,time());
and it works fine. Thx for the suggestion
Post Reply