Much more elegant code Re: [PHP] Remembering which option in a SELECT tag the user had chosen
Webapprentice
webapprentice at onemain.com
Wed Jan 29 00:10:40 EST 2003
I am truly a novice. haha. This is so elegant.
I don't have access to a database, so I'll go with your array structure.
The PHP server is < 4.1. I have to go with
$HTTP_POST_VARS["timeChoice"] (Explicit reference)
I was originally doing this using #3 of your notes 9type out each if
statement), but that was so inefficient that there had to be another way.
I can load the date values in an array and cycle through that.
Thank you Justin for the tip. Let me attempt to work along the idea you
provided.
--Stephen
Justin French wrote:
>For starters, let's assume that these dates are stored in a database or
>array, so that we can do everything through a loop.
>
>I'll just do an array of 4 dates to prove the point:
>
><?
>$dates = array(
> 'Saturday August 23, 10:00 PM - 12:00 AM',
> 'Sunday August 24, 12:00 AM - 02:00 AM',
> 'Sunday August 24, 12:00 PM - 02:00 PM',
> 'Sunday August 24, 02:00 PM - 04:00 PM'
> );
>?>
><select name="timeChoice">
><?
>foreach($dates as $key => $val)
> {
> if( (isset($_POST['timeChoice'])) && ($val == $_POST['timeChoice']) )
> { $sel = ' SELECTED'; }
> else
> { $sel = ''; }
> echo "<option value='{$val}'{$sel}>{$val}</option>";
> }
>?>
></select>
>
>1. untested code
>
>2. assumes register globals are OFF and PHP > 4.1 ... change
>$_POST['timeChoice'] to $timeChoice if you're old fashioned :)
>
>3. if you don't have these dates in an array or db table, then you will have
>to manually type out each if statement, to check whether the value you're
>putting into the option matches a value in $_POST['timeChoice']
>
>4. change $_POST['timeChoice'] to $_GET['timeChoice'] if your <form> method
>is GET.
>
>
>Justin
>
>
>
>
>
More information about the talk
mailing list