Copy link to clipboard
Copied
I've noticed that when I'm inserting a dynamic drop down select menu where the options are built from a Recordset, the last record in the recordset is always selected and the menu always opens upwards. It also always ignores the fact if I throw in a static option and have that set at the default "Currently Selected" option, it will ignore that also.
In the picture below, I want the static option of "Select a Medium" to appear before a user hits the drop down select and once they hit it, I want the drop down's options to be listed in an ascending alphabetical order not to mention I'd like the menu to drop downward instead of opening upwards. (The records are currently in order in the database in an alphabetical order)
Here is the code btw
<form action="" method="get"><select name="test">
<option value="" <cfif (isDefined("rsMediums.mediumID") AND "" EQ rsMediums.mediumID)>selected="selected"</cfif>>Select a Medium</option>
<cfoutput query="rsMediums">
<option value="#rsMediums.mediumID#" <cfif (isDefined("rsMediums.mediumID") AND rsMediums.mediumID EQ rsMediums.mediumID)>selected="selected"</cfif>>#rsMediums.mediumName#</option>
</cfoutput>
</select></form>
Copy link to clipboard
Copied
<form action="" method="get"><select name="test">
<option value="" <cfif (isDefined("rsMediums.mediumID") AND "" EQ rsMediums.mediumID)>selected="selected"</cfif>>Select a Medium</option>
<cfoutput query="rsMediums">
<option value="#rsMediums.mediumID#" <cfif (isDefined("rsMediums.mediumID") AND rsMediums.mediumID EQ rsMediums.mediumID)>selected="selected"</cfif>>#rsMediums.mediumName# </option>
</cfoutput>
</select></form>
Remove the CFIF conditions from the your OPTION tags. If you view source, you'll probably see that all of the options but the first are selected, which is why the last option is appearing as the default.
Also, if you want the options in alphabetical order, use the appropriate ORDER BY clause in your SQL query:
SELECT ...
FROM ...
WHERE ...
ORDER BY mediumName
Dave Watts, CTO, Fig Leaf Software
Copy link to clipboard
Copied
I normally troubleshoot if/else problems like this:
if (i got what I expected)
output yes
else
output no plus what I actually got
Copy link to clipboard
Copied
I dont know why select is opening up, but you are telling the select to select the option if rsMediums.mediumID EQ rsMediums.mediumID, and all cases of rsMediums.mediumID will equal themselves. Most likely there is some other variable you are trying to match (like #usersFavoriteMedium#:
Thus:
<option value="#rsMediums.mediumID#" <cfif (isDefined("usersFavoriteMedium") AND usersFavoriteMedium EQ rsMediums.mediumID)>selected="selected"</cfif>>#rsMediums.mediumName# </option>
And in that case the first option would want to be selected (no cfif) so if usersFavoriteMedium was not defined or didn't match any mediums from query, the user would be told to Select a Medium.
Copy link to clipboard
Copied
J_Tremain requirements:
1) I want the drop down's options to be listed in an ascending alphabetical order ...(The records are currently in order in the database in an alphabetical order)...
2) I want the static option of "Select a Medium" to appear before a user hits the drop down select ...
3) I'd like the menu to drop downward instead of opening upwards.
1) Even if the records are in order in the database, the order in the result-set may be all mushed up. If you have access to the query, then add an ORDER BY clause to it, as Dave has suggested. If you don't have access to the query, then create a sorted copy of it, using query-of-a-query, and use that copy instead.
<cfquery name="rsMediums_sorted" dbtype="query">
select *
from rsMediums
order by mediumName
</cfquery>
2) By default, the first listed option appears at the top of the drop-down list. You could adapt your code as follows. This also was first suggested by from Dave. I am repeating it here to explain my suggestion about query-of-query.
<form action="" method="get">
<select name="test">
<option value="" selected>Select a Medium</option>
<!--- if you applied query-of-a-query, then use rsMediums_sorted instead of rsMediums --->
<cfoutput query="rsMediums">
<option value="#rsMediums.mediumID#">#rsMediums.mediumName# </option>
</cfoutput>
</select>
</form>
3) The options in the select tag are designed to "drop down" by default. As yours is "dropping up", it can only mean that something else is affecting the page styling. Check your CSS code(if you use any), and the positioning of the other HTML elements on the page.
Having said this, I would suggest you use the following code. It gives you more possibilities:
<cfform method="get">
<!--- if you applied query-of-a-query, then use rsMediums_sorted instead of rsMediums --->
<cfselect name="test" query="rsMediums" display="name" value="ID" queryPosition="below">
<option value="" selected>Select a Medium</option>
</cfselect>
</cfform>