For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > List select query.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author List select query.
Richard Brooks

2005-11-29, 6:57 pm

I've got a country list which, after selecting and the 'submit' button
is clicked, I need for the list to re-display the selected list element,
the values are for stored database referencing only and not for display.


<select name="country">
<option value="<?php echo $country;?>" SELECTED><?php echo
$country;?></option>
<option Value="AF">Afghanistan</option>
<option Value="AL">Albania</option>
<option Value="DZ">Algeria</option>
.....
<option Value="ZW">Zimbabwe</option>
</Select>


It's one of those 'mental block' days but what I need is for the list to
display the selected element text and not the value so that the user
can check their details.

Many thanks,


Richard.

Oli Filth

2005-11-29, 6:57 pm

Richard Brooks wrote:
> I've got a country list which, after selecting and the 'submit' button
> is clicked, I need for the list to re-display the selected list element,
> the values are for stored database referencing only and not for display.
>
>

<...SNIP...>
>
> It's one of those 'mental block' days but what I need is for the list to
> display the selected element text and not the value so that the user
> can check their details.
>


http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1

Richard Brooks

2005-11-29, 6:57 pm

Oli Filth wrote:
> Richard Brooks wrote:
>
>
> <...SNIP...>
>
>
>
> http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1


Read it through many times today and cannot find the named element I'm
looking for.

A very many thanks for your help!


Richard.

Hilarion

2005-11-29, 6:57 pm

> I've got a country list which, after selecting and the 'submit' button
> is clicked, I need for the list to re-display the selected list element,
> the values are for stored database referencing only and not for display.
>
>
> <select name="country">
> <option value="<?php echo $country;?>" SELECTED><?php echo
> $country;?></option>
> <option Value="AF">Afghanistan</option>
> <option Value="AL">Albania</option>
> <option Value="DZ">Algeria</option>
> ....
> <option Value="ZW">Zimbabwe</option>
> </Select>



Try this:

<select name="country">
<option Value="AF"<?php if ($country=='AF') echo ' SELECTED'; ?>>Afghanistan</option>
<option Value="AL"<?php if ($country=='AL') echo ' SELECTED'; ?>>Albania</option>
<option Value="DZ"<?php if ($country=='DZ') echo ' SELECTED'; ?>>Algeria</option>
....
<option Value="ZW"<?php if ($country=='ZW') echo ' SELECTED'; ?>>Zimbabwe</option>
</Select>

Better one, which assumes that you have your country names and codes in
some array in this format:

$countries = array(
'AF' => 'Afghanistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
...
'ZW' => 'Zimbabwe'
);

would be:

<select name="country">
<?php
foreach( $countries as $code => $name )
{
printf(
"<option value=\"%s\"%s>%s</option>\n",
htmlspecialchars( $code ),
(($code === $country)?' SELECTED':''),
htmlspecialchars( $name )
);
}
?>
</select>


Hilarion
Richard Brooks

2005-11-29, 6:57 pm

Hilarion wrote:
[snipped][color=darkred]
>
> Try this:
>
> <select name="country">
> <option Value="AF"<?php if ($country=='AF') echo ' SELECTED';
> ?>>Afghanistan</option>
> <option Value="AL"<?php if ($country=='AL') echo ' SELECTED';
> ?>>Albania</option>
> <option Value="DZ"<?php if ($country=='DZ') echo ' SELECTED';
> ?>>Algeria</option>
> ....
> <option Value="ZW"<?php if ($country=='ZW') echo ' SELECTED';
> ?>>Zimbabwe</option>
> </Select>
>
> Better one, which assumes that you have your country names and codes in
> some array in this format:
>
> $countries = array(
> 'AF' => 'Afghanistan',
> 'AL' => 'Albania',
> 'DZ' => 'Algeria',
> ...
> 'ZW' => 'Zimbabwe'
> );
>
> would be:
>
> <select name="country">
> <?php
> foreach( $countries as $code => $name )
> {
> printf(
> "<option value=\"%s\"%s>%s</option>\n",
> htmlspecialchars( $code ),
> (($code === $country)?' SELECTED':''),
> htmlspecialchars( $name )
> );
> }
> ?>
> </select>
>
>
> Hilarion


Thanks Hilarion!

I've got yet another variation of the above somewhere in my archive
tonnage but I had one of those 'elderly' moments and needed a quick answer.

Thanks again!

Richard.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com