Link to home
Start Free TrialLog in
Avatar of Mike Waller
Mike WallerFlag for United States of America

asked on

loop over form field (select) options

I'm using the following code but I need to loop over each record and select the one that is in the db.  however, it needs to display all the records too. how would I do that?
<CFQUERY NAME="getlinks" DATASOURCE="dns">
S sortorder
F tablename
order by sortorder asc
</CFQUERY>
 
<form method="post" action="page.cfm" name="sortorder">
Sort Order:<br />
<cfloop query="getlinks">
<select name="getorder" id="getorder">
<cfoutput>
<option value="#getlinks.sortorder#">#getlinks.sortorder#</option>
</cfoutput>
</select>
</form>

Open in new window

Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

the code you have will generate a separate <select> list for each record returned by your query - is that what you intend it to do? i think what you have in mind is more like:

<select name="getorder" id="getorder">
<cfoutput query="getlinks">
<option value="#sortorder#">#sortorder#</option>
</cfoutput>
</select>

i am not sure which record is the "one that is in the db" that you want to pre-select? what's the criteria for pre-selecting a record?

Azadi
Avatar of Mike Waller

ASKER

actually, I need seperate select fields for each iteration.
oh, ok. do you mean then that you need X number of <select> lists, each with X number of options in it? if so, this should do it:

<cfoutput query="getlinks">
<select name="getorder#getlinks.currentrow#" id="getorder#getlinks.currentrow#">
<cfloop query="getlinks">
<option value="#getlinks.sortorder#">#getlinks.sortorder#</option>
</cfloop>
</select>
</cfoutput>

the code to "select the one that is in the db" will depend on where that value is coming from (i.e. another query, url/form variable, etc) and which format that value is in (i.e. is it a single value or a delimited list of values)...

Azadi
Azadi, I need to populate each select list with all the records but select the one that's in the db.
ASKER CERTIFIED SOLUTION
Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm trying to do the following but the value does not get selected.
<CFQUERY NAME="getlinks" DATASOURCE="#application.datasource#">
S SM.*, ST.id, ST.templateNameID, ST.sitenameID
F #application.dbowner1#.site#placement#menu SM, siteNameID_templateNameID ST
W SM.stID = ST.id and ST.templateNameID = '#application.TemplateNameID#' and ST.sitenameID = '#application.siteNameID#'
order by [order] asc, SM.id asc
</CFQUERY>
 
<slect name="getorder" id="getorder">
<CFQUERY NAME="getlinks2" DATASOURCE="#application.datasource#">
S SM.*, ST.id, ST.templateNameID, ST.sitenameID
F #application.dbowner1#.site#placement#menu SM, siteNameID_templateNameID ST
W SM.stID = ST.id and ST.templateNameID = '#application.TemplateNameID#' and ST.sitenameID = '#application.siteNameID#'
order by [order] asc, SM.id asc
</CFQUERY>
<cfoutput>
	<cfloop query="getlinks2">
		<cfif getlinks2.order eq getlinks.order>
		<option value="#getlinks2.order#" selected>#getlinks2.order#</option>
		<cfelse>
		<option value="#getlinks2.order#">#getlinks2.order#</option>
		</cfif>
	</cfloop>
</cfoutput>
</slect>

Open in new window

Thx!