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

COwebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

azadisaryevCommented:
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
0
COwebmasterAuthor Commented:
actually, I need seperate select fields for each iteration.
0
azadisaryevCommented:
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
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

COwebmasterAuthor Commented:
Azadi, I need to populate each select list with all the records but select the one that's in the db.
0
azadisaryevCommented:
ok. so i assume your getlinks query returns all possible sort values. but which one of them is this "the one that's in the db"? where's this defined? in which variable/query? and what does it look like - is it one value or is it a list of values (i.e. after a user has selected several of sort options)?

and, do you want to "select the one that's in the db" in all of your <select> lists? or just one? or some of them?

please explain.

basic code to pre-select an option in a select list based on a value of some variable is:

<cfloop query="getlinks">
<option value="#getlinks.sortorder#" <cfif getlinks.sortorder eq somevariable>selected</cfif>>#getlinks.sortorder#</option>
</cfloop>

but, depending on what that somevariable is, it may have to be done differently (especially if that somevariable is a list of values and not just one value)... and, also, depending on which select list(s) you want to pre-select an option in...

Azadi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
COwebmasterAuthor Commented:
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

0
COwebmasterAuthor Commented:
Thx!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.