Connecting Tech Pros Worldwide Forums | Help | Site Map

Can a default value be specified for a drop-down list?

deko
Guest
 
Posts: n/a
#1: Jul 17 '05
I populate a drop-down list like this:

echo "<select>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";

Is it possible to have the selection in the drop-down list default to a
particular value? For example, let's say the complete list of values looks
like this:

65.162.31.155
210.213.149.6
68.190.20.83
130.202.234.254
68.122.35.157

When the page is loaded, I want 68.190.20.83 to appear as the selected value
in the drop-down list. Can this be done? Any suggestions or examples
welcome!

Thanks!



Simon Stienen
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Can a default value be specified for a drop-down list?


deko <www-dot-clearpointsystems-dot-com@nospam.com> wrote:[color=blue]
> I populate a drop-down list like this:
>
> echo "<select>";
> foreach ( $ip_list as $var )
> {
> echo "<option>";
> echo $var;
> echo "</option>";
> }
> echo "</select>";
>
> Is it possible to have the selection in the drop-down list default to a
> particular value? For example, let's say the complete list of values looks
> like this:
>
> 65.162.31.155
> 210.213.149.6
> 68.190.20.83
> 130.202.234.254
> 68.122.35.157
>
> When the page is loaded, I want 68.190.20.83 to appear as the selected value
> in the drop-down list. Can this be done? Any suggestions or examples
> welcome!
>
> Thanks![/color]

This is an HTML-issue, not PHP. You are looking for <option selected>
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
deko
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Can a default value be specified for a drop-down list?


> This is an HTML-issue, not PHP. You are looking for <option selected>

10-4. I will research <option selected> and see if I can find a solution.


Theo
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Can a default value be specified for a drop-down list?


"deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote in
news:SnY4d.1980$nj.773@newssvr13.news.prodigy.com:
[color=blue]
> I populate a drop-down list like this:
>
> echo "<select>";
> foreach ( $ip_list as $var )
> {
> echo "<option>";
> echo $var;
> echo "</option>";
> }
> echo "</select>";
>
> Is it possible to have the selection in the drop-down list default to
> a particular value? For example, let's say the complete list of
> values looks like this:
>
> 65.162.31.155
> 210.213.149.6
> 68.190.20.83
> 130.202.234.254
> 68.122.35.157
>
> When the page is loaded, I want 68.190.20.83 to appear as the selected
> value in the drop-down list. Can this be done? Any suggestions or
> examples welcome!
>
> Thanks!
>
>
>[/color]

There is probably a better way to do this since this was just a quick and
dirty method to make it work for me.....

HTML requires the particular menu choice to be listed as 'selected'. So
you can make an array of choices (listed by name instead of number) that
match the ones in the HTML, default them to nothing. and set the proper
one to 'selected'. In the HTML, add php code to each choice. The ones
that are blank will be ignored. The one with 'selected' will be the
choice.

the starting php
$select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>"");
$select['porche'] = 'selected';

The normal HTML unselected option line looks like this
<option value="none">Porche</option>

The line that is selected via HTML is
<option value="none" selected>Porche</option>

The line using a php array is
<option value="none" <? print $select['porche']; ?> >None</option>

If porche was set as 'selected' when setting up the array, it will match
example #2. If not, it will match #1. You can expand on this by using
choices in a database instead of creating the array manually.

If there IS a better way I will also be interested ;o)
Simon Stienen
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Can a default value be specified for a drop-down list?


Theo <invalid@noemail.com> wrote:[color=blue]
> "deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote in
> news:SnY4d.1980$nj.773@newssvr13.news.prodigy.com:
>[color=green]
>> I populate a drop-down list like this:
>>
>> echo "<select>";
>> foreach ( $ip_list as $var )
>> {
>> echo "<option>";
>> echo $var;
>> echo "</option>";
>> }
>> echo "</select>";
>>
>> Is it possible to have the selection in the drop-down list default to
>> a particular value? For example, let's say the complete list of
>> values looks like this:
>>
>> 65.162.31.155
>> 210.213.149.6
>> 68.190.20.83
>> 130.202.234.254
>> 68.122.35.157
>>
>> When the page is loaded, I want 68.190.20.83 to appear as the selected
>> value in the drop-down list. Can this be done? Any suggestions or
>> examples welcome!
>>
>> Thanks!
>>
>>
>>[/color]
>
> There is probably a better way to do this since this was just a quick and
> dirty method to make it work for me.....
>
> HTML requires the particular menu choice to be listed as 'selected'. So
> you can make an array of choices (listed by name instead of number) that
> match the ones in the HTML, default them to nothing. and set the proper
> one to 'selected'. In the HTML, add php code to each choice. The ones
> that are blank will be ignored. The one with 'selected' will be the
> choice.
>
> the starting php
> $select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>"");
> $select['porche'] = 'selected';
>
> The normal HTML unselected option line looks like this
> <option value="none">Porche</option>
>
> The line that is selected via HTML is
> <option value="none" selected>Porche</option>
>
> The line using a php array is
> <option value="none" <? print $select['porche']; ?> >None</option>
>
> If porche was set as 'selected' when setting up the array, it will match
> example #2. If not, it will match #1. You can expand on this by using
> choices in a database instead of creating the array manually.
>
> If there IS a better way I will also be interested ;o)[/color]

-----BEGIN PHP CODE BLOCK-----
$options = array(
'option1',
'option2',
'option3',
'option4'
);
$selected = 1; // option2

for ($i=0, $len=sizeof($options); $i<$len; $i++)
echo '<option value="'.$i.
(($i==$selected) // select this option?
? ' selected>'
: '>').
$options[$i].'</option>';
------END PHP CODE BLOCK------

--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
deko
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Can a default value be specified for a drop-down list?


> > There is probably a better way to do this since this was just a quick
and[color=blue][color=green]
> > dirty method to make it work for me.....
> >
> > HTML requires the particular menu choice to be listed as 'selected'. So
> > you can make an array of choices (listed by name instead of number) that
> > match the ones in the HTML, default them to nothing. and set the proper
> > one to 'selected'. In the HTML, add php code to each choice. The ones
> > that are blank will be ignored. The one with 'selected' will be the
> > choice.
> >
> > the starting php
> > $select = array("none"=>"", "porche"=>"", "mustang"=>"",[/color][/color]
"corvette"=>"");[color=blue][color=green]
> > $select['porche'] = 'selected';
> >
> > The normal HTML unselected option line looks like this
> > <option value="none">Porche</option>
> >
> > The line that is selected via HTML is
> > <option value="none" selected>Porche</option>
> >
> > The line using a php array is
> > <option value="none" <? print $select['porche']; ?> >None</option>
> >
> > If porche was set as 'selected' when setting up the array, it will match
> > example #2. If not, it will match #1. You can expand on this by using
> > choices in a database instead of creating the array manually.
> >
> > If there IS a better way I will also be interested ;o)[/color]
>
> -----BEGIN PHP CODE BLOCK-----
> $options = array(
> 'option1',
> 'option2',
> 'option3',
> 'option4'
> );
> $selected = 1; // option2
>
> for ($i=0, $len=sizeof($options); $i<$len; $i++)
> echo '<option value="'.$i.
> (($i==$selected) // select this option?
> ? ' selected>'
> : '>').
> $options[$i].'</option>';
> ------END PHP CODE BLOCK------[/color]

hmmm... that seems like a lot of code.

The below code seems to work - that is, the drop-down list shows $select_ip
as the selected choice when the page loads, and all the other IPs are still
available for selection in the list.

echo "<select>";
echo "<option selected value=".$select_ip.">".$select_ip."</option>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";


Simon Stienen
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Can a default value be specified for a drop-down list?


deko <www-dot-clearpointsystems-dot-com@nospam.com> wrote:[color=blue][color=green][color=darkred]
>>> There is probably a better way to do this since this was just a quick[/color][/color]
> and[color=green][color=darkred]
>>> dirty method to make it work for me.....
>>>
>>> HTML requires the particular menu choice to be listed as 'selected'. So
>>> you can make an array of choices (listed by name instead of number) that
>>> match the ones in the HTML, default them to nothing. and set the proper
>>> one to 'selected'. In the HTML, add php code to each choice. The ones
>>> that are blank will be ignored. The one with 'selected' will be the
>>> choice.
>>>
>>> the starting php
>>> $select = array("none"=>"", "porche"=>"", "mustang"=>"",[/color][/color]
> "corvette"=>"");[color=green][color=darkred]
>>> $select['porche'] = 'selected';
>>>
>>> The normal HTML unselected option line looks like this
>>> <option value="none">Porche</option>
>>>
>>> The line that is selected via HTML is
>>> <option value="none" selected>Porche</option>
>>>
>>> The line using a php array is
>>> <option value="none" <? print $select['porche']; ?> >None</option>
>>>
>>> If porche was set as 'selected' when setting up the array, it will match
>>> example #2. If not, it will match #1. You can expand on this by using
>>> choices in a database instead of creating the array manually.
>>>
>>> If there IS a better way I will also be interested ;o)[/color]
>>
>> -----BEGIN PHP CODE BLOCK-----
>> $options = array(
>> 'option1',
>> 'option2',
>> 'option3',
>> 'option4'
>> );
>> $selected = 1; // option2
>>
>> for ($i=0, $len=sizeof($options); $i<$len; $i++)
>> echo '<option value="'.$i.
>> (($i==$selected) // select this option?
>> ? ' selected>'
>> : '>').
>> $options[$i].'</option>';
>> ------END PHP CODE BLOCK------[/color]
>
> hmmm... that seems like a lot of code.
>
> The below code seems to work - that is, the drop-down list shows $select_ip
> as the selected choice when the page loads, and all the other IPs are still
> available for selection in the list.
>
> echo "<select>";
> echo "<option selected value=".$select_ip.">".$select_ip."</option>";
> foreach ( $ip_list as $var )
> {
> echo "<option>";
> echo $var;
> echo "</option>";
> }
> echo "</select>";[/color]

You are not required to put the selected value in the first place.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Christopher-Robin
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Can a default value be specified for a drop-down list?


$list=array(...); // any hash or indexed list
$select=2; // key of selected item in $list

foreach($list as $id => $item)
echo "<option value=\"$id\"".
(($id==$select)?(" selected"):("")).
">$item</option>";


Closed Thread