[PHP] Defaults in drop down list...

2003-01-06 Thread Steven Kallstrom
I have a drop down list with all fifty states. very common.  I conjured
up a way to store the value when you return to edit the form, but there
most be an easier way either in html, or in php.  Here is what I
currently have.
 
select name=state
option value=AL{$stateselected['AL']}Alabama/option
option value=AK{$stateselected['AK']}Alaska/option
option value=AZ{$stateselected['AZ']}Arizona/option
.
/select
 
$stateselected['$state'] is an array that stores the state that was
selected on the prior form.  is there an easier way, to have a default
state picked out of this drop down list.???
 
Thanks,
 
Steven Kallstrom



Re: [PHP] Defaults in drop down list...

2003-01-06 Thread Justin French
What I would do, and this is one of those write it once, use it a 1000
times things, is store all your states in an array.

?
$states = array(
'AL' = 'Alabama',
'AK' = 'Alaska',
'AZ' = 'Arizona'
);
?

Then, you can loop through that array to build your form element:

?
echo 'select name=state';
foreach($states as $code = $fullName)
{
echo option value='{$code}'{$fullName}/option;
}
echo '/select';
?

MUCH less code eh?

Then, to have the pre-selected element selected again, I'll assume you have
a value like $state or $selectedState, with a value of 'AZ' for example.

?
$selectedState = 'AZ'; //usually grabbed from a database I assume?

echo 'select name=state';
foreach($states as $code = $fullName)
{
if($stateSelected == $code) { $sel = ' SELECTED'; } else { $sel = ''; }
echo option value='{$code}'{$sel}{$fullName}/option;
}
echo '/select';
?

The result of which would be something like:

select name=state
option value='AL'Alabama/option
option value='AK'Alaska/option
option value='AZ' SELECTEDArizona/option
/select


Season to taste :)

Justin French




on 07/01/03 4:54 AM, Steven Kallstrom ([EMAIL PROTECTED]) wrote:

 I have a drop down list with all fifty states. very common.  I conjured
 up a way to store the value when you return to edit the form, but there
 most be an easier way either in html, or in php.  Here is what I
 currently have.
 
 select name=state
 option value=AL{$stateselected['AL']}Alabama/option
 option value=AK{$stateselected['AK']}Alaska/option
 option value=AZ{$stateselected['AZ']}Arizona/option
 .
 /select
 
 $stateselected['$state'] is an array that stores the state that was
 selected on the prior form.  is there an easier way, to have a default
 state picked out of this drop down list.???
 
 Thanks,
 
 Steven Kallstrom
 


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




Re: [PHP] Defaults in drop down list...

2003-01-06 Thread Rick Widmer
At 11:54 AM 1/6/03 -0600, Steven Kallstrom wrote:


select name=state
option value=AL{$stateselected['AL']}Alabama/option
option value=AK{$stateselected['AK']}Alaska/option
option value=AZ{$stateselected['AZ']}Arizona/option
.
/select

$stateselected['$state'] is an array that stores the state that was
selected on the prior form.  is there an easier way, to have a default
state picked out of this drop down list.???



Write a function that paints selects:

$States = array( 'AL' = 'Alabama', ...


function PaintSelect( $Name, $Values, $Selected ) {

echo SELECT NAME=\$Name\\n;

reset( $Values );
while( list( $Index, $Value ) = each( $Values )) {
   $Selected = ( $Selected == $Index ) ? ' SELECTED' : '';
   echoOPTION VALUE=\$Index\$SELECTED$Value/OPTION\n'
   }

echo /SELECT\n;

}


Then call lt like this ...

State: ? PaintSelect( 'State', $States, $stateselected['$state'] ) ?BR


Rick




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