Re: <OPTION

From: Date: Wed, 25 Oct 2006 23:09:17 +0000
Subject: Re: <OPTION
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-243633@lists.php.net to get a copy of this message
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

« previous php.general (#243633) next »