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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic List box/drop downs

Status
Not open for further replies.

jacob94

Technical User
Joined
Dec 5, 2005
Messages
161
Location
US
Hello,
I have a view that displays the following information
ID NAME Selection
1 jason jason
1 john jason

The selection field gets updated from another page. they can chose jason or john and click submit; hence the above jason was the choice.

I simply need to create another drop down showing the selection (jason) as the default, then jason and john in the drop down...

Can someone help me with a small example. this has to be easy, but i just spent 4 hours searching and can't get it to work right. sorry to bother with something this simple...it is not for me....
 
Are the following assumptions accurate?

* All games have exactly 2 players
* All games will have exactly one player named "Home" and one player named "Visitor"

 
here is the db as posted above:
gameid VisitorID HomeID Selection
1 RED BLUE RED
2 Orange Purple Orange
3 Grey Teal Grey
ETC ETC ETC ETC

all games have 2 choices (visitorid and HomeID)
All games will have one selection .

 
OK - here's what we do in pseudo-code:

1. Have a main loop to step through each row in the recordset

2. Output the the drop down

3. Check each value to see if it needs to be selected

4. Go back to step 1.

<%
'Main Loop
Do While Not rs.EOF
%>

<select name="menu">

<option value="<% = rs("VisitorID") %>">
<% = rs("VisitorID") %>
</option>

<option value="<% = rs("HomeID") %>">
<% = rs("HomeID") %>
</option>

</select>

<br><br>

<%

rs.MoveNext
Loop

rs.Close
Set rs=Nothing
Set DBConn=Nothing
%>

If you can get that code working you should end up with multiple drop down menus - once we've got that working we can concentrate on the correct values being selected by default when the page loads.

Let us know how you get on!
 
When specifying which value is selected by default when the page loads it is important to make sure you put your If...End If statement in the right place.

You cannot say (in pseudo-code)

If Selection is red

<option value="red" SELECTED>Red</option>

End If

<option value="blue">Blue</option>


Because that will only produce a proper drop down menu with both options if Selection is red. If the selection is blue your drop down list will only have one value.

Instead you should:

1. Write the first option tag but don't put the > on the end.

2. See what the value of Selection is

3. If Selection matches the value of the first option (VisitorID) then write "SELECTED" otherwise don't write anything

4. Close the option tag with a >

5. Write the second option tag but don't put the > on the end.

6. See what the value of Selection is

7. If Selection matches the value of the first option (HomeID) then write "SELECTED" otherwise don't write anything

8. Close the option tag with a >

 
Emozley:
I thank you for all the help. I do have the drop down working properly but I can't get it to default to the selection. I do not understand how to perform your second post.

 
No worries - please can you post your latest code for the drop down menu - not all of it, basically Do While Not rs.EOF through to Loop.

cheers
 
Look at my code above, it does the selection.
 
<%
'Open the database.
call OpenDB()

sql = "select * from view"
set rs = DbConn.Execute(sql)

do while not rs.EOF



if rs.Fields("GameID").Value = 1 THEN

strGameID1 = strGameID1 & "<option value=" & rs("VisitorID") & ">" & rs("VisitorID") & "</option>"
strGameID1 = strGameID1 & "<option value=" & rs("HomeID") & ">" & rs("HomeID") & "</option>"
end if

'Loop through the data.
rs.MoveNext
loop

set rs = nothing
dbconn.close
%>

In the HTML FORM I HAVE THE DROP DOWN
<select name="GameID1" size="1" id="GameID1">
<%=strGameID1%>
</select>

 
Sorry we are in danger of going round in circles here - your code cannot be made to work in it's current form.

Neither is it scalable - if you update the database you will have to re-code the page.

Please strongly consider using either my code or sheco's, you will save yourself many hours of stress!

cheers

Ed
 
It is essential that you have your If...End If statement INSIDE the <option> tag otherwise 50% of the time it will not display the full drop down list.
 
The code below is 100% scalable - you will see that all of the <select> elements are named uniquely - GameID1, GameID2, GamedID3 etc. Both options are ALWAYS displayed and we check to see the value of Selection is to say whether or not it is selected.

<%

Do While Not rs.EOF

%>

<select name="GameID<% = rs("GameID") %>">

<option value="<% = rs("VisitorID") %>" <% If rs("VisitorID")=rs("Selection") Then Response.Write("SELECTED") %>>

<% = rs("VisitorID") %>

</option>


<option value="<% = rs("HomeID") %>" <% If rs("HomeID")=rs("Selection") Then Response.Write("SELECTED") %>>

<% = rs("HomeID") %>

</option>

</select>

<%

rs.MoveNext
Loop

rs.Close
Set rs=Nothing
Set dbConn=Nothing

%>
 
It still does not select the default selection...


<%
'Open the database.
call OpenDB()

sql = "select * from view"
set rs = DbConn.Execute(sql)
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<FORM name=brackets>

<%

Do While Not rs.EOF

%>

<select name="GameID<% = rs("GameID") %>">

<option value="<% = rs("VisitorID") %>" <% If rs("VisitorID")=rs("Selection") Then Response.Write("SELECTED") %>>

<% = rs("VisitorID") %>

</option>


<option value="<% = rs("HomeID") %>" <% If rs("HomeID")=rs("Selection") Then Response.Write("SELECTED") %>>

<% = rs("HomeID") %>

</option>

</select>

<%

rs.MoveNext
Loop

rs.Close
Set rs=Nothing
Set dbConn=Nothing

%>


<br>
<input name="SubmitButton" type="submit" id="SubmitButton" value="Submit">

</FORM>

</body>
</html>
 
Perhaps something wrong with your database code... or perhaps the database does not yet contain the value at the time that the recordset is created.

Next time the unexpected behavior appears, do a "View Source" in your browser. This will show the HTML that was sent from your ASP to the browser... copy the pertinant portion and post it here.
 
The database is updated and the selection field is populated.

<body>

<FORM name=brackets>



<select name="GameID1">

<option value="1" SELECTED>

1

</option>


<option value="2" SELECTED>

2

</option>

</select>



<select name="GameID2">

<option value="3" >

3

</option>


<option value="4" >

4

</option>

</select>




<br>
<input name="SubmitButton" type="submit" id="SubmitButton" value="Submit">

</FORM>


The numbers you see for home and visitor id are just for example sake. It really will have a team in there. Like red team or blue team.
 
The HTML above could only be created by the ASP listed farther above in the cirmcumstance that HomeID = VisitorID
 
I do not understand sheco?

I did exaclty as you posted...
 
Please can you copy and paste the full contents of the table in the database with column headers.

cheers
 
I ran the code on my PC and it errored because of the table being called 'View'. Are you using SQL Server or Access? I am suprised you did not get the same problem.

 
After renaming the table 'View' to 'Viewing' and updating the SQL SELECT statement to "SELECT * FROM Viewing" I set up the DB and used the following data:

GameID HomeID VisitorID Selection
1 RED BLUE RED
2 GREEN ORANGE GREEN

It works on my PC - the only thing I would say is that if Selection is blank the value selected by default is VisitorID. If Selection does have a value then it defaults to the correct option.
 
emozley, my mistake, I GOT IT. I had changed a column data type by mistake but I fixed it. Wow, thatnks to the both of you! Stars all around... I learned a ton.

I have another question. I need to place 8 of these boxes onto a table grid. I know they get generated in the loop by the game id. How would I go about placing the boxes onto a grid?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top