Forum Moderators: coopster

Message Too Old, No Replies

drop down dates

         

nshack31

6:20 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



I have the following code

<select name=\"month\" >
<option value=\"1\">January</option>
<option value=\"2\">February</option>
<option value=\"3\">March</option>
<option value=\"4\">April</option>
<option value=\"5\">May</option>
<option value=\"6\">June</option>
<option value=\"7\">July</option>
<option value=\"8\">August</option>
<option value=\"9\">September</option>
<option value=\"10\">October</option>
<option value=\"11\">November</option>
<option value=\"12\">December</option>
</select>

as you can see date is stored as an integer when this is sent to the DB. But i then need to read this value again! So i use..

//store DB value as an option
$data_month .=<<<HTML
<option value="$month">$month</option>
<option>------</option>
HTML;

//display drop down box with DB value
<select name=\"month\" >
$data_month
<option value=\"1\">January</option>
<option value=\"2\">February</option>
<option value=\"3\">March</option>
<option value=\"4\">April</option>
<option value=\"5\">May</option>
<option value=\"6\">June</option>
<option value=\"7\">July</option>
<option value=\"8\">August</option>
<option value=\"9\">September</option>
<option value=\"10\">October</option>
<option value=\"11\">November</option>
<option value=\"12\">December</option>
</select>

The problem is that I need $data_month to be shown as a Name (eg August) and not and integer. Other than use 12 'If' statements, is there an easy way of doing this?

Thankyou

nshack31

6:42 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Ah! sorted using

$monthconv = date("F", mktime(0, 0, 0, $month, 1, 0));

and then using that as the select option

coopster

9:13 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good thinking. Glad you got it sorted.

dreamcatcher

11:28 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just one quick observation which might be useful. Why not store these values in an array? If you are using code more than once, a simple loop is a lot quicker to code.

Just a thought.

dc

coopster

11:50 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's true dc, and good point. But you could also use strftime() [php.net]. The latter option allows you to setlocale() [php.net] if you ever need to build the list in other languages dynamically. All-in-all, there are quite a few options -- but still, this one will indeed work. If performance becomes a problem, nshack31, dc has offered an option for you :)

nshack31

9:11 am on Mar 4, 2005 (gmt 0)

10+ Year Member



Thanks guys, I'd use an array but my knowledge of PHP is not great and arrays is something im not up to scratch on! :(

dreamcatcher

10:01 am on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Arrays can be very useful. Here`s a quick example using months like you have:

$months = array(
"01" => "January",
"02" => "February",
"03" => "March",
"04" => "April",
"05" => "May",
"06" => "June",
"07" => "July",
"08" => "August",
"09" => "September",
"10" => "October",
"11" => "November",
"12" => "December"
);

You could pop this in an include file called 'months.php'. Then on the page you want to display your array, you could use:


@include('months.php');

<select name="month" >
<?php

foreach ($months as $key => $value)
{
echo "<option value='$key'>$value</option>\n";
}

?>
</select>

Hope that helps.

dc

nshack31

11:30 am on Mar 4, 2005 (gmt 0)

10+ Year Member



I'll try that thankyou! :)