Re: Re: <OPTION

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



...and if you wanted to go an extra step you could try an alternate
syntax of the exact same code:

<?php
   foreach (range(1, 31) as $day)
       print '<option value="' . $day . '"' . ($selected_day_of_month
== $day ? ' selected' : '') . '>' . $day . '</option>';
?>

Either way, Stut is correct. Don't type this out 31 times unless you
really really really like to type. ;-)
- Joe

On 10/25/06, Stut <stuttle@xxxxxxxxx> wrote:
Ron Piggott (PHP) wrote:
> I am creating a form right now.  I am using the html SELECT tag.
>
> <SELECT NAME="day_of_month">
>
> The most number of days in a month is 31
>
> I want the output to be <OPTION SELECTED>## based on the value of
> $selected_day_of_month variable.  For the days of the month where the
> number is not selected the output I am desiring is
>
> <OPTION>1
> <OPTION>2
> etc.
>
> to be generated through PHP code
>
> I already have a value the user submitted in a variable named
> $selected_day_of_month from being stored in a mySQL table
>
> Does anyone know how to do this in a few commands?  I am wanting to
> simplify
>
> <?
> if ( $selected_day_of_month == "1" ) }
> <OPTION SELECTED>1
> } ELSE {
> <OPTION>1
> }
>
> if ( $selected_day_of_month == "2" ) }
> <OPTION SELECTED>2
> } ELSE {
> <OPTION>2
> }
>
> ?>
>
> etc.

Dang that's painful!! Try this...

<?php
     foreach (range(1, 31) as $day)
     {
         print '<option value="'.$day.'"';
         if ($selected_day_of_month == $day)
             print ' selected';
         print '>'.$day.'</option>';
     }

?>

I added a value to the options (you knew that was missing right?).
Obviously you need to output the <select ...> and </select> outside this
loop.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux