Forum Moderators: coopster

Message Too Old, No Replies

building select for months of year

how to build a select from current month onwards

         

wheelie34

1:16 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys

I am trying to build a select in php, it must make current month "selected" AND this is where I am stuck, should only generate the current month and the remainder of the current years months, heres what I have so far.

$montharray = array('Jan' => 'January','Feb' => 'February', 'Mar' => 'March', 'Apr' => 'April',
'May' => 'May', 'Jun' => 'June', 'Jul' => 'July', 'Aug' => 'August', 'Sep' => 'September',
'Oct' => 'October', 'Nov' => 'November', 'Dec' => 'December');
$currentmonth = date("M");
print "<select name=\"month\" size=\"1\">";
foreach ( $montharray as $key=>$val )
if ($key == $currentmonth)
{
print"<option value=\"$key\" selected>$val</option> \n";
}
else
{
print"<option value=\"$key\">$val</option> \n";
}
print "</select>";

How can I tell it to only create current and following months?

Thanks

coopster

4:44 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would just use a one-based array [php.net] (it fits the date [php.net] function well) and substring the month value for my options.
$montharray = array( 
1 => 'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
$monthNumber = date('n');
print "<select name=\"month\" size=\"1\">";
for ($m = $monthNumber; $m <= 12; $m++) {
$monthAbbreviation = substr($montharray[$m], 0 , 3);
if ($m == $monthNumber) {
print"<option value=\"$monthAbbreviation\" selected>$montharray[$m]</option> \n";
} else {
print"<option value=\"$monthAbbreviation\">$montharray[$m]</option> \n";
}
}
print "</select>";

wheelie34

5:02 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coopster

I had already changed to 01=>January etc then on the result spage did a case switch to convert it back to jan etc, your method takes further load of the server.

thank you

I have another select in the form,which I have just started working on, its a multiple select, it grabs a bunch of pre formatted dates from a database, I also want to tell that to only list dates from now until the end of the year, the dates in the db are like this Jan 12/13/14, which is Fri/Sat/Sun, its the same dates I was searching with the original select.

What method would work for that, to show it I am using

$sql="select * from dates";
$result = mysql_query($sql, $dblink) or die("System down");
while ($newArray = mysql_fetch_array($result)){
$val = $newArray['dates'];
print "<option value=\"$val\">$val</option>";
}

which displays them fine, now I need to only show current and following months of the year, I just need to know WHAT method to use, like you pointed out one based array and the method you would use, what would you do for this one?

Thanks again

coopster

10:41 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not so sure I understand the database column entry layout. Why not just have a single row for each date? Why the slash-separated dates?