On Thu, 30 Sep 2004 22:16:46 +0200 (CEST), [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi,
> to create a list of all months in drop-down menu I use this code:
>
> <?php
> $month_names = array(1=>'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
> 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
> ?>
>
> <select name="QuoteMonth">
> <option value=""></option>
> <?php
> for($i=1; $i<=12; $i++)
> {
> if(date('m') == $i) $selected_QuoteMonth = 'SELECTED';
> else $selected_QuoteMonth = '';
> echo "<option value=$i $selected_QuoteMonth> $i </option>";
> }
> ?>
> </select>
>
> Is there any better way?
Here's how I might re-do your code (notes below):
$months = array ('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
print "<select name='QuoteMonth'>";
foreach ($months as $number => $month)
{
$selected = "";
if (date ('n') == $number)
{
$selected = "selected='selected'";
}
print "<option value='$number' $selected>$month</option>";
}
print "</select>";
Notes (warning, many personal biases included):
1. Quote all HTML attributes.
2. Use XHTML-compatible markup for "selected" attribute, nutty as it looks.
3. Use the month name, not the number, for your display values.
4. If you want a blank "option" entry, you might as well include it in
your array.
5. Avoid iterating through arrays with C-style "for" loops; "foreach"
is cleaner.
6. Don't continually switch between PHP and HTML modes; either write
PHP that prints HTML, or use templates.
7. Use blocks in "if" statements.
8. Use Whitesmiths brace style :)
pb
--
paul bissex, e-scribe.com -- database-driven web development
413.585.8095
69.55.225.29
01061-0847
72�39'71"W 42�19'42"N
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php