View Full Version : Option value 0

03-25-2009, 09:07 PM
I have a drop down list filling itself from my database. All of the items (names, actually) show up nicely, however I want the first item to be Choose..., or Select..., not the first item/name on the list. When I try to enter <option value="0">Choose... anywhere in the code, with or without the end tag </option>, the word Choose... appears above EVERY item/name in the list! I know how to do this with a manual list, but not with one drawing from a database. Does anyone out there know how to do this?

Here's the code:

<label><b>Select Counselor: </b></label><select name="counselor" size="1">
do {
<option value="<?php echo $row_counselor['name']?>"<?php if (!(strcmp($row_counselor['name'], $row_counselor['name'])))?>><?php echo $row_counselor['name'];?></option>
} while ($row_counselor = mysql_fetch_assoc($counselor));
$rows = mysql_num_rows($counselor);
if($rows > 0) {
mysql_data_seek($counselor, 0);
$row_counselor = mysql_fetch_assoc($counselor);


03-25-2009, 09:28 PM
This isn't something in PHP, its an HTML thing.
Use the <optgroup> tag, and add a label for "Choose". This adds a display for the choose, but makes it unselectable, which means you never need to worry about it:

<label><b>Select Counselor: </b></label><select name="counselor" size="1">
<optgroup label="Choose">
<?php // do php stuff ?>

Something like that.

03-25-2009, 09:38 PM
Hi Fou-Lu,
Thanks. I did try your suggestion, and it did put a Choose at the top of my list, but the first name on the list still appears in the list box, not Choose. I don't see Choose until I drop down the list, then I have to nudge up to the top to see it listed first in the list. Maybe it can't be done.

03-25-2009, 09:51 PM
Just put your choose option before the do..while loop.

Though I have to ask, isn't it obvious to your user that he/she needs to choose? Why even put that in the list? If you want the listbox to not have anything selected initially, use javascript to select the -1 index of the listbox.

03-26-2009, 01:46 PM
Thank you, Fumigator for your response. Yes, that was the answer, to put the choose option before the loop. I had been putting it inside. (I'm still really new to this.)

The reason that I want a choose option is that the output from the form goes to a confirmation page, and if a selection is not made for certain entries (this is one of them), I want a message to appear that tells the user that they need to go back and make a selection. Without the Choose option, which gives the 0 output, the name of the first counselor appears, which may or may not be the correct entry. This way if the output is 0, I can tell them to go make a choice. Thanks again for your help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum