Re: One more form question
| From: | Kevin Stone | Date: | Fri, 03 Jan 2003 21:10:05 +0000 |
| Subject: | Re: One more form question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-130153@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: "David Chamberlin" <dwc@nautiboy.com>
To: <php-general@lists.php.net>
Sent: Friday, January 03, 2003 1:27 PM
Subject: [PHP] One more form question
> OK, last one was answered quickly and successfully (thank you!), so
> here's another one.
>
> Is there an easy way to set something in a select list to be selected?
> Right now I'm doing a real brute-force method. e.g.,
>
> echo "<td><select name=\"disp_address\">";
> $choices = array( 'pub' => 'On Public Page',
> 'members' => 'Only on Members Page',
> 'nodisp' => 'Do not Display' );
> foreach ( $choices as $key => $choice ) {
> $selected = '';
> if ( strcmp( $key, $member_info->display_address ) == 0 ) {
> $selected = 'selected';
> }
> echo "<option value=\"$key\" $selected>$choice";
> }
> echo '</select></td>';
>
> I've got to imagine there's a better way ....
>
> Thanks,
> Dave
Okay let me take another hack at one of your questions and maybe I won't
screw up this time. ;-)
PHP has no functional control over the HTML content. You could make it
easier on yourself by writing a function that outputs 'selected' or null
instead of coding a separate conditional for each form element, but other
than that this is the only way to do it.
-Kevin