473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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!
Jul 17 '05 #1
7 6578
deko <ww*******************************@nospam.com> wrote:
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!


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
Jul 17 '05 #2
> 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.
Jul 17 '05 #3
"deko" <ww*******************************@nospam.com> wrote in
news:Sn***************@newssvr13.news.prodigy.com:
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!


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)
Jul 17 '05 #4
Theo <in*****@noemail.com> wrote:
"deko" <ww*******************************@nospam.com> wrote in
news:Sn***************@newssvr13.news.prodigy.com:
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!


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)


-----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
Jul 17 '05 #5
> > 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)


-----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------


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>";
Jul 17 '05 #6
deko <ww*******************************@nospam.com> wrote:
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)


-----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------


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>";


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
Jul 17 '05 #7
$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>";
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Vandervies | last post by:
If I feed this to g++: -------- int foo(int i=42); int foo(int i=42) { return i; } -------- It says (with -W -Wall -ansi -pedantic):
0
by: inquirydog | last post by:
Hi- I am using xml to hold configuration data for a project, and using schema to define what the configuration file should look like. I wanted to get some advice on an intelligant way to...
11
by: Rithish | last post by:
If I have something like, <TABLE><TR><TD STYLE="'Font-Family:Arial'">Arial Text</TD></TR></TABLE> What would be the height of the cell? I believe it would be corresponding to the font used......
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
5
by: skyloon | last post by:
hi, my structure table in database: Amount float(53) not null default 0 when i try to run his script: alter table ABC alter column Amount float(53) null it can only set the Amount to allow...
1
by: aj | last post by:
I used the Create Database Wizard in CC to create a quick test database. However, I screwed up when it asked about what database path (DFTDBPATH) to use. Rather than using the DFTDBPATH that is...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
3
by: Beemer Biker | last post by:
I started seeing huge amounts of error messages such as "Validation (XHTML 1.0 Transitional):" Element schema is not supported". There was nothing wrong with that for 3 weeks and suddenly there...
4
by: teddysnips | last post by:
A few weeks ago a client asked me to add a column to a table so I created this script: ALTER TABLE dbo.tblIndividual ADD fldRenewalStatus BIT NOT NULL CONSTRAINT fldRenewalStatus_Default DEFAULT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.