473,326 Members | 2,108 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,326 software developers and data experts.

Drop Down Lists

I have a drop down list with the town options 'Bury' and 'Ipswich' as
shown below.

When selecting one of the options, its value is passed to variable
$townsearch.

How do I change the drop down list so the option selected last time is
then the current one shown in the drop down box?
<body>

<?php $townsearch = $_get['town']; ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

<select size="1" name="town">

<option>ipswich</option>
<option>bury</option>
&nbsp;
</select>&nbsp;

<input type="submit" value="GO" />

</form>

Many thanks

Alec

May 16 '06 #1
12 2964
Rik
Alec wrote:
How do I change the drop down list so the option selected last time is
then the current one shown in the drop down box? <?php $townsearch = $_get['town']; ?>
//euhm, $_GET['town'].....
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<select size="1" name="town">
<?php
$towns = array('ipswich','bury', 'eanske');
foreach($towns as $town){
$selected = (isset($_GET['town'])&&$_GET['town']==$town)? '
selected="selected"':'';
print("<option value=\"$town\"$selected>$town</option>");
}
<option>ipswich</option>
<option>bury</option>
</select>
<input type="submit" value="GO" />
</form>


Grtz
--
Rik Wasmus
May 16 '06 #2
EXCELLENT!!!!!

Many many thanks Rik

Alec

May 16 '06 #3
Very effifient. Mine is a monster - I did a query inside a query:

$locationquery = mysql_query ("SELECT * FROM locations");
$comparequery = mysql_query ("SELECT location FROM reservations WHERE
id=" . $_GET['id']);
while ($compareresult = mysql_fetch_array ($comparequery)) {
while ($locationrow = mysql_fetch_array ($locationquery)) {
echo ("<option");
if ($locationrow['name'] == $compareresult['location']) { //the
magic happens here
echo (" selected");
}
echo (">$locationrow[name]</option>");
}
}

Because $comparequery fetches only one value, any way to retrieve it
BEFORE going into the forst loop?

May 16 '06 #4
Alec wrote:
I have a drop down list with the town options 'Bury' and 'Ipswich' as
shown below.

When selecting one of the options, its value is passed to variable
$townsearch.

How do I change the drop down list so the option selected last time is
then the current one shown in the drop down box?
<body>

<?php $townsearch = $_get['town']; ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

<select size="1" name="town">

<option>ipswich</option>
<option>bury</option>
&nbsp;
</select>&nbsp;

<input type="submit" value="GO" />

</form>

Many thanks

Alec


Here is one way:
<?php $townsearch = $_GET['town']; ?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<select name="town">
<?php
$choices = array('ipswitch', 'bury');
foreach( $choices as $choice ) {
if( $choice == $townsearch ) {
printf("<option selected>%s</option>\n", $choice);
} else {
printf("<option>%s</option>\n", $choice);
}
}
?>
</select>
<input type="submit" value="GO" />
</form>

Here is another:
<?php $townsearch = $_GET['town']; ?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<select name="town">
<?php
$choices = array('ipswitch', 'bury');
foreach( $choices as $choice ) {
printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
'selected' : '', $choice);
}
?>
</select>
<input type="submit" value="GO" />
</form>

-david-

May 16 '06 #5
Rik
TristaSD wrote:
Very effifient. Mine is a monster - I did a query inside a query:

$locationquery = mysql_query ("SELECT * FROM locations");
$comparequery = mysql_query ("SELECT location FROM reservations WHERE
id=" . $_GET['id']);
while ($compareresult = mysql_fetch_array ($comparequery)) {
while ($locationrow = mysql_fetch_array ($locationquery)) {
echo ("<option");
if ($locationrow['name'] == $compareresult['location']) {
//the magic happens here
echo (" selected");
}
echo (">$locationrow[name]</option>");
}
}

Because $comparequery fetches only one value, any way to retrieve it
BEFORE going into the forst loop?

First of all, you don't need the first while loop,. I assume it's only on
record that has that certain id, so you can just:
$compareresult = mysql_fetch_array ($comparequery)
while ($locationrow = mysql_fetch_array ($locationquery)) {
if ($locationrow['name'] == $compareresult['location']) {
echo (" selected");
}
}

It's possible in one query:

if(isset($_GET['id'])&&$_GET['id']!=''){
$id = mysql_rel_escape_string(GET['id']);
$query = "SELECT x.name, IF(x.name=y.location, ' selected=\"selected\"',
'') as `selected` FROM locations x, reservations y WHERE y.id=$id";
} else {
$query = "SELECT name, '' as `selected` FROM locations";
}

$locationquery = mysql_query($query);
while ($locationrow = mysql_fetch_array ($locationquery)){
printf('<option%s>%s</option>', $locationrow['selected'],
$locationrow['name']);
}

I'm not enterily sure it's quicker though... My MySQL is still not as it
should be.

Grtz,
--
Rik Wasmus
May 16 '06 #6
Rik
David Haynes wrote:
printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
'selected' : '', $choice);


Damn, didn't know one could nest the () ? : ; syntax like that.. nice.

Grtz,
--
Rik Wasmus
May 16 '06 #7
Rik
Rik wrote:
I'm not enterily sure it's quicker though... My MySQL is still not as
it should be.


And allthough it's rusty, some pointers for efficiency:
1. If you're not going to use the enumerated array, use mysql_fetc_assoc().
2. Only use the * in "SELECT * FROM table" if strictly necessary: The less
fields the faster:

Querying a 4 field, 900 rows table for all data 600 times:

Selecting 3 fields:
7.1682569980621

Selecting all fields by name:
7.5627238750458

Selecting *:
8.8992791175842

Grtz,
--
Rik Wasmus
May 16 '06 #8
Rik wrote:
David Haynes wrote:
printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
'selected' : '', $choice);


Damn, didn't know one could nest the () ? : ; syntax like that.. nice.

Grtz,


Yep!
One of my favorites is:

printf("aBoolean is %s\n", $aBoolean ? 'true' : 'false');
-david-

May 16 '06 #9
Carved in mystic runes upon the very living rock, the last words of Rik
of comp.lang.php make plain:
Alec wrote:
How do I change the drop down list so the option selected last time is
then the current one shown in the drop down box?

<?php $townsearch = $_get['town']; ?>


//euhm, $_GET['town'].....
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<select size="1" name="town">


<?php
$towns = array('ipswich','bury', 'eanske');
foreach($towns as $town){
$selected = (isset($_GET['town'])&&$_GET['town']==$town)? '
selected="selected"':'';


The SELECTED attribute is a boolean attribute, and does not require a
value (though it can have one)

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 16 '06 #10
'Bury' or 'Ipswich'? Are these the only options? :-(
Surely things aren't that bad.

May 16 '06 #11
Rik
Alan Little wrote:
The SELECTED attribute is a boolean attribute, and does not require a
value (though it can have one)

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2


I'm strictly XHTML at the moment, so if you'd forgive me this quircks :-)

Grtz,
--
Rik Wasmus
May 16 '06 #12
Carved in mystic runes upon the very living rock, the last words of Rik of
comp.lang.php make plain:
Alan Little wrote:
The SELECTED attribute is a boolean attribute, and does not require a
value (though it can have one)

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2


I'm strictly XHTML at the moment, so if you'd forgive me this quircks :-)


Ah. No criticism intended, just a <!-- --> :)

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 17 '06 #13

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

Similar topics

3
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
1
by: csgraham74 | last post by:
Hi Guys, I am currently building a web aplication in ASP.Net. On the onload event of a page a have a function to populate the many drop down lists on my page. I have one parameter table which...
7
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
1
by: accyboy1981 | last post by:
Hi, I new to C# so please forgive me if this is simple. I've got 2 drop down lists the first is hard coded with data where as the second is populated from a database. The options that appear in...
0
by: kajir | last post by:
Hi, I am new at using ASP.Net 2.0. I have various drop down lists on my master page. They refer to an SQL database. I also have a menu on the master page. I can select the values in the drop...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
22
by: Archanak | last post by:
Hi, I am using 2-level CSS Drop Down Menu in my perl/CGI program. here is the code. #!c:/perl/bin/perl.exe use CGI qw(:standard);
3
by: jcassan | last post by:
Hello folks. I am new to these forums and have something, which has been stumping me for little while. I am using pspell to spellcheck a scrolling textbox (textarea) containing user input. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.