The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Add an item to dropdown list
Discuss Add an item to dropdown list in the PHP Development forum on Dev Shed. Add an item to dropdown list PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 19th, 2012, 02:55 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 7 h 25 m 59 sec
Reputation Power: 0
|
|
|
Add an item to dropdown list
Hello, I have a while lopp generated dropdown list, and I would like to add "Select" at the top, so as soon as they see the list the first option is Select, not the items inside the list.
Here is my dropdown menu:
PHP Code:
<select id="select2" name="select2">
<?php
$ids = 0;
while($rows = mysql_fetch_assoc($run)) {
echo "<option value=$ids>".$rows['Short']."</option>";
$ids++;
}
?>
</select>
|

December 19th, 2012, 03:40 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
so the first item should be selected? Isn't that the default behaviour?
If it isn't, just check the counter:
|

December 19th, 2012, 03:54 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 7 h 25 m 59 sec
Reputation Power: 0
|
|
|
Yes it is selected, but I would like to add 1 predefined option in there called "select", and then continue with displaying other items..
|

December 19th, 2012, 04:00 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Well, I don't know where this name "select" appears. But it works just like I said above: if the current element has that name, you add
Code:
selected="selected"
to the option tag.
|

December 19th, 2012, 04:04 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 7 h 25 m 59 sec
Reputation Power: 0
|
|
You know...
There are items inside my dropdown menu:
Item 1
Item 2
Item 3
Item 4
......
And they are generated by the while loop:
PHP Code:
<select id="select2" name="select2">
<?php
$ids = 0;
while($rows = mysql_fetch_assoc($run)) {
echo "<option value=$ids>".$rows['Short']."</option>";
$ids++;
}
?>
</select>
Now as soon as user comes to the website they see Item 1 in dropdown menu. I want to add one more "item" called Select or Select item to this dropdown menu.. hope this is clear 
|

December 19th, 2012, 04:08 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Sorry, I misunderstood you.
In this case simply echo that option above the loop:
PHP Code:
echo "<option value="0" selected="selected">Select</option>";
(Make sure you don't accept this option as a valid selection.)
|

December 19th, 2012, 04:11 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 7 h 25 m 59 sec
Reputation Power: 0
|
|
No problem, you are helping me
Thanks, this solved my problem 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|