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

Search feature with list boxes 1

Status
Not open for further replies.

nissan240zx

Programmer
Joined
Jun 26, 2005
Messages
280
Location
US
Hello All,
I am trying to make a Search page which uses List boxes.
And i want the items in the list box to show up from a database field.
And when user select a particular item from the list..show all matching records.
When clicked a particular record show its details.
Can anyone help me guide to the right source.
Are there any source codes or applications available to make this work.

Thanks in advance...
Nissan

Nissan 240 ZX...best Nissan Ever
 
here you go: below is a sample code to get you started

Code:
<html>
<body>
<%
dim rs, conn, sql
set rs=server.createobject("adodb.recordset")
set conn=server.createobject("adodb.connection")
conn.open "your connectionstring here"
sql = "your sql string here"
rs.open sql, conn
%>
<form name="myform" method="POST" action="searchresults.asp">
<select size="1" name="mydropbox">
<option>-Select One-</option>
		<%
                While NOT rs.EOF 
                 %>
                 <Option value="<%=rs("yourfield")%>"><%=rs("yourfield")%></option>
                 <%
                 rs.MoveNext
                 Wend
                 %>
		</select>
<input type="submit" name="submit" value="search">
</form>
</body>
</html>

try it out and post with any questions that you have
-DNG
 
also dont forget to close all your objects in the end...

-DNG
 
Thank you very..the values showup right in the list.
Could u also please help me out with the searchresults.asp.
Before that let me explain.
The list box values are coming from table 1 and the matching records should be displayed from table 2.

Please help.


Nissan 240 ZX...best Nissan Ever
 
ok no problem...
1. Now you have your form set up with the dropdown
2. When the user selects an item from the dropdown list and clicks submit
----this is done----
3.you need to grab the user selection on the searchresults.asp
4. search for all the records from table depending on this user selection
5. display the results back to user
-----we need to do this------

here is the code for searchresults.asp page

Code:
<html>
<body>
<%
'declare variables
dim rs, conn, sql, userselection

'grab user selected item from the dropdown
userselection=request.form("mydropbox")

'set all our objects
set rs=server.createobject("adodb.recordset")
set conn=server.createobject("adodb.connection")

'open connection string
conn.open "your connectionstring here"
'write our sql statement
sql = "your sql string here"
'here is a sample sql of how it should look

'sql="SELECT * FROM table2 WHERE myfield='"&userselection&"';"


rs.open sql, conn


IF rs.EOF AND rs.BOF then
response.write "sorry, no records found"
else

do until rs.EOF
'display all your results here, for example:
response.write rs("field1")
rs.movenext
loop

'close all your objects here
%>
</body>
</html>

hope that helps....post back if you have any other questions...

-DNG
 
here is the other version of the same code...it displays search results in the table format...

Code:
<html>
<body>
<%
'declare variables
dim rs, conn, sql, userselection

'grab user selected item from the dropdown
userselection=request.form("mydropbox")

'set all our objects
set rs=server.createobject("adodb.recordset")
set conn=server.createobject("adodb.connection")

'open connection string
conn.open "your connectionstring here"
'write our sql statement
sql = "your sql string here"
'here is a sample sql of how it should look

'sql="SELECT * FROM table2 WHERE myfield='"&userselection&"';"


rs.open sql, conn

Response.Write "<table border=1>"
    If Not rs.EOF Then
        Response.Write "<tr>"
        For x = 0 To rs.Fields.Count - 1
            Response.Write "<th>" & rs(x).Name & "</th>"
            Response.Flush()
        Next
        Response.Write "</tr>"
        
        Do While Not rs.EOF
            Response.Write "<tr>"
            For x = 0 To rs.Fields.Count - 1
                Response.Write "<td>" & rs(x)& "&nbsp;</td>"
            Next
            Response.Write "</tr>"
            rs.MoveNext
        Loop
    End If
    Response.Write "</table>"

'close all your objects here
%>
</body>
</html>

-DNG
 
i forgot

End if in the first code i provided...put End If after the word Loop.

-DNG
 
Did the 240 use the same 3L V6 as the maxima and 300?
 
Mine is a 1973 NISSAN, 240ZX, V8, 350cu in, 350 turbo

Nissan 240 ZX...best Nissan Ever
 
DotNetGnat..thsi is just awesome

Nissan 240 ZX...best Nissan Ever
 
no problem...glad to be of help...let me know if you encounter any problems while using that code...

-DNG
 
LOL! My eyes saw 240 SX

You know.... the little one from the 1980s!


I guess Z is cooler than S



 
Hello DNG,
Everything works "sweet".
Had one question though - if I want to present the values in a nice table format with proper size (height and width), how do I approach it.

Thanks n advance,
Nissan

Nissan 240 ZX...best Nissan Ever
 
When I say "if I want to present the values in a nice table format" what I really mean is..show the content in sections.
For example if the database values are
Product ID, Product Name, Description, Vendor Name, Contact Informaiton.

This should be shown in section:
Product Informaiton will have:
Product ID, Product Name, Description

Vendor Information section will have:
Vendor Name, Contact Informaiton

Please help....



Nissan 240 ZX...best Nissan Ever
 
if that is the case then you can use this:

Code:
<%
.....
rs.open sql, conn


IF rs.EOF AND rs.BOF then
response.write "sorry, no records found"
else
%> 'closing the tag here

<table border="1", cellspacing="1", cellpadding="1">
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Description</th>
<th>Vendor Name</th>
<th>Contact Information</th>

<% 'open tag here again
do until rs.EOF
%> 'close tag
<tr>
<td align="center"><font size="2" face="verdana"><%=rs("ProductId")%></td>
<td align="center"><font size="2" face="verdana"><%=rs("ProductName")%></td>
<td align="center"><font size="2" face="verdana"><%=rs("ProductDesc")%></td>
<td align="center"><font size="2" face="verdana"><%=rs("VendorName")%></td>
<td align="center"><font size="2" face="verdana"><%=rs("ContactInfo")%></td>
<%
rs.movenext
loop

'close all your objects here

%>

i have just used only some attributes of <td> and <table> and <font>...you can use what ever you want...but i hope you got the point...

-DNG
 
hmmm...dont forget to put end if in the end...

Code:
<%
rs.movenext
loop
[red]end if[/red]
'close all your objects here
[red]rs.close
set rs=nothing
con.close
set con=nothing[/red]
%>

post back if you have any questions...

-DNG
 
You are one amazing, patient person.
Truly wonderful experience.

Thanks a million.


Nissan 240 ZX...best Nissan Ever
 
Hello DNG,
One last question regarding this topic
How do I link a image to the search results
I was trying to do something like this

<td align="center"><img src="images/<%=rs ("productImg")%>" align="left"></td>

All my images (jpgs) are stored in folder called Images and my database has a field that stores the image name (without a .jpg),

Please advice.


Nissan 240 ZX...best Nissan Ever
 
you can try this:

imagename="/images/"&rs("productImg")&".JPG"
'for testing print it out
response.write imagename

<td align="center"><img src="<%=imagename%>" align="left"></td>

or simply...

<td align="center"><img src="<%="/images/"&rs("productImg")&".JPG"%>" align="left"></td>

always do a response.write to see if the the pathname is correct...

you image tag should look like this

<IMG SRC="imagename">

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top