Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate Entry in db populated drop down menu? 1

Status
Not open for further replies.

newcow

Technical User
Feb 24, 2004
80
0
0
CA
I am using the follow code in my EDIT form but I have a dup entry. This is on my EDIT page so I need the code to populate the selected with what the current field is in the db. What did I need to do to remove the dup entry?

<select size="1" name="VN" tabindex="1">
<cfoutput><option value="#VN#" selected="selected">#VName#</option></cfoutput>
<option value="1">Car</option>
<option value="2">Truck</option>
<option value="3">Van</option>
<option value="4">SUV</option>
<option value="5">RV</option>
<option value="6">Bike</option>
<option value="7">Boat</option>
<option value="8">Sled</option>
</select>

I read both of the follow faqs:
And I also read the comments in but I still can't figure it out.

newcow
 
U can try to put select option into an array variable, then populate the select box using that array. I've typed the sample code below.

<cfset arrItem = arrayNew(1)>
<cfset arrItem[1] = "Car">
.
.
.
<cfset arrItem[8] = "Sled">

<select name="VN">
<cfloop index="i" from="1" to="8">
<option value="#i#" <cfif i eq VN>selected</cfif>>#arrItem#</option>
</cfloop>
</select>
 
Actually, all you need are a few cfif statements in your option tags.
Code:
<select size="1" name="VN" tabindex="1">
<cfoutput>
  <option value="1"<cfif VN EQ "1"> selected</cfif>>Car</option>
  <option value="2"<cfif VN EQ "2"> selected</cfif>>Truck</option>
  <option value="3"<cfif VN EQ "3"> selected</cfif>>Van</option>
  <option value="4"<cfif VN EQ "4"> selected</cfif>>SUV</option>    
  <option value="5"<cfif VN EQ "5"> selected</cfif>>RV</option>
  <option value="6"<cfif VN EQ "6"> selected</cfif>>Bike</option>
  <option value="7"<cfif VN EQ "7"> selected</cfif>>Boat</option>
  <option value="8"<cfif VN EQ "8"> selected</cfif>>Sled</option>
</cfoutput>
  </select>
Of course, all of this would be much easier if were pulling the info from a database instead of having to hard code all of the values. Anyway, this should get you started.



Hope This Helps!

Ecobb

"My work is a game, a very serious game." - M.C. Escher
 
Thanks Ecobb; Yes that one works very good. It is quite simple also.
Also, in this case I don't mind typeing in all the options instead of pulling it from a db, but I have some more work to do on my search and that would work good to be able to have a pull down menu for the vehicles that are in the db.
I will go over the FAQs you posted for SSonnier and ask that question when I get there.
Tiono I tried yours also but couldn't get it to work, I am sure it was just me though. Thanks.
newcow.
 
Why couldn't the DISTINCT function be used in the query?
<CFQUERY NAME="name" Datasource="datasource>
Select DISTINCT field_name
FROM table_name
</cfquery>
 
Why couldn't the DISTINCT function be used in the query?
<CFQUERY name="name" Datasource="datasource">
Select DISTINCT field_name
FROM table_name
</cfquery>
 
The actual problem in this case wasn't so much the results returned from the query as it was the combination of hard coding and dynamically populating the values in the <option> tags.

Here, all of the options were hard coded, then another option was added that would be populated from the query results. Since whatever was returned from the query would already have been hard coded as an option, there would be 2 listings for that value in the select box. Instead of dynamically populating an option tag from the query results, it's now set up to dynamically populate the SELECTED attribute of the existing option tag that matches the query result.


Hope This Helps!

Ecobb

"My work is a game, a very serious game." - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top