Avatar billede rashid Praktikant
15. februar 2004 - 22:58 Der er 4 kommentarer og
1 løsning

søgemaskine som Eksperten

Hej jeg har brug for at få lavet en søge ting til min hjemmeside ligesom den der findes her på eksperten. Er der nogle "Eksperter" der kan hjælpe ?

siden jeg er ved at lave er til noget køb/salg/bytte, og tingene er inddelt i kategorier samt underkategorier. Jeg vil eks. kunne søge på farve, årgang, model etc.
Avatar billede rashid Praktikant
16. februar 2004 - 01:27 #1
Det skal gerne være noget med nogle dropdownlist-bokse, hvor man på denne måde kan lave select * from kategori where farve like "sort" and model like "mountainbike"
Avatar billede hnteknik Novice
16. februar 2004 - 10:12 #2
Prøv at søg under javascript - jeg mener der er en hel del eksempler på det
Avatar billede rashid Praktikant
18. februar 2004 - 23:39 #3
Hej Jeg fandt et eksempel - kan nogen hjælpe med at tilrette det i henhold til mit behov? altså søgning i en db med kategori, subkategori, farve, model, pris (min/max)
på forhånd tak. (har nogen brug for hele scriptet kan I skrive en mailadresse (hotmail))

<%@ Language=VBScript %>
<!-- METADATA TYPE="typelib" UUID="00000200-0000-0010-8000-00AA006D2EA4" NAME="ADO Type Library"-->

<%
Option Explicit
Response.Buffer = True        'Turn buffering on
Response.Expires = -1        'Page expires immediately

'Constants
Const MIN_PAGESIZE = 5        'Minimum pagesize
Const MAX_PAGESIZE = 100    'Maximum pagesize
Const DEF_PAGESIZE = 70        'Default pagesize

'Variables
Dim objCn                    'ADO DB connection object
Dim objRs                    'ADO DB recordset object
Dim blnWhere                'True/False for have WHERE in sql already
Dim intRecord                'Current record for paging recordset
Dim intPage                    'Requested page
Dim intPageSize                'Requested pagesize
Dim sql                        'Dynamic sql query string

'Create objects
Set objCn = Server.CreateObject("ADODB.Connection")
Set objRs = Server.CreateObject("ADODB.Recordset")

'Set/initialize variables
intRecord = 1
blnWhere = False

'-Get/set requested page
intPage = MakeLong(Request("page"))
If intPage < 1 Then intPage = 1

'-Get/set requested pagesize
If IsEmpty(Request("pagesize")) Then    'Set to default
    intPageSize = DEF_PAGESIZE
Else
    intPageSize = MakeLong(Request("pagesize"))
    'Make sure it fits our min/max requirements   
    If intPageSize < MIN_PAGESIZE Then
        intPageSize = MIN_PAGESIZE
    ElseIf intPageSize > MAX_PAGESIZE Then
        intPageSize = MAX_PAGESIZE
    End If
End If

'-Build dynamic sql
sql = "SELECT Tbladd.*, Tblstate.*, Tblcategory.*, Tblinterest.* FROM Tblstate INNER JOIN (Tblinterest INNER JOIN (Tblcategory INNER JOIN Tbladd ON Tblcategory.ID = Tbladd.Category) ON Tblinterest.ID = Tbladd.Interest) ON Tblstate.ID = Tbladd.State "

'--ID (exact search only)
If Not IsEmpty(Request("id")) Then
    If IsNumeric(Request("id")) Then
        blnWhere = True 'Set where to true
        sql = sql & "WHERE "
        sql = sql & "(Tbladd.ID = " & CStr(CLng(Request("id"))) & ") "
    End If
End If


'--Category (exact search only)
If Not IsEmpty(Request("category")) Then
    If IsNumeric(Request("category")) Then
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql = sql & "(Tbladd.Category = " & CStr(CLng(Request("category"))) & ") "
    End If
End If


'--Interest (exact search only)
If Not IsEmpty(Request("interest")) Then
    If IsNumeric(Request("interest")) Then
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql = sql & "(Tbladd.Interest = " & CStr(CLng(Request("interest"))) & ") "
    End If
End If


'--State (exact search only)
If Not IsEmpty(Request("state")) Then
    If IsNumeric(Request("state")) Then
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql = sql & "(Tbladd.State = " & CStr(CLng(Request("state"))) & ") "
    End If
End If


'--Keyword (parital search only) search both title and text
If Not IsEmpty(Request("keyword")) Then
    Dim strKeyword
    strKeyword = Trim(Request("keyword"))
   
    If strKeyword <> "" Then
        'Test for WHERE
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql = sql & "(Tbladd.Title LIKE '%" & Replace(strKeyword, "'", "''") & "%' OR Tbladd.Text LIKE '%" & Replace(strKeyword, "'", "''") & "%') "
    End If
End If


'--Price (minimum)
If Not IsEmpty(Request("minprice")) Then
    If IsNumeric(Request("minprice")) Then
        Dim dblMinPrice
        dblMinPrice = CDbl(Request("minprice"))

        'Test for WHERE
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql    = sql & "(Tbladd.Price >= " & CStr(dblMinPrice) & ") "
    End If
End If
       
'--Price (maximum)
If Not IsEmpty(Request("maxprice")) Then
    If IsNumeric(Request("maxprice")) Then
        Dim dblMaxPrice
        dblMaxPrice = CDbl(Request("maxprice"))

        'Test for WHERE
        If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql    = sql & "(Tbladd.Price <= " & CStr(dblMaxPrice) & ") "
    End If
End If   

'--Show only records with picture
If (Request("pic")) = "ON" Then
    If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
        sql    = sql & " FileData OR Picturelink = " & "''" & " "
End If


If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
sql = sql & "Tbladd.Random LIKE " & "0"


'--Sort By Field
sql = sql & " ORDER BY "
Select Case Trim(LCase(Request("sortby")))
    Case "text":    sql = sql & "Tbladd.Text "
    Case "title":    sql = sql & "Tbladd.Title "
    Case "price":    sql = sql & "Tbladd.Price "
    Case Else:        sql = sql & "Tbladd.ID "
End Select


'--Sort Order
Select Case Trim(LCase(Request("sortorder")))
    Case "asc":        sql = sql & "ASC"
    Case Else:        sql = sql & "DESC"
End Select
'--Dynamic sql finished


'Create and open connection object
With objCn
    .CursorLocation = adUseClient
    .ConnectionTimeout = 15
    .CommandTimeout = 30
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Server.MapPath("market.mdb") & ";"
    .Open
End With


'Create and open recordset object
With objRs
    .ActiveConnection = objCn
    .CursorLocation = adUseClient
    .CursorType = adOpenForwardOnly
    .LockType = adLockReadOnly
    .Source = sql
    .PageSize = intPageSize
    .Open
    Set .ActiveConnection = Nothing    'Disconnect the recordset
End With


'Creates a long value from a variant, invalid always set to zero
Function MakeLong(ByVal varValue)
    If IsNumeric(varValue) Then
        MakeLong = CLng(varValue)
    Else
        MakeLong = 0
    End If
End Function

'Returns a neatly made paging string, automatically configuring for request
'variables, regardless of in querystring or from form, adjust output to your needs.
Function Paging(ByVal intPage, ByVal intPageCount, ByVal intRecordCount)
    Dim strQueryString
    Dim strScript
    Dim intStart
    Dim intEnd
    Dim strRet
    Dim i

    If intPage > intPageCount Then
        intPage = intPageCount
    ElseIf intPage < 1 Then
        intPage = 1
    End If
   
    If intRecordCount = 0 Then
        strRet = "No Records Found"
    ElseIf intPageCount = 1 Then
        strRet = "End of hits"
    Else
        For i = 1 To Request.QueryString.Count
            If LCase(Request.QueryString.Key(i)) <> "page" Then
                strQueryString = strQueryString & "&"
                strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Key(i)) & "="
                strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Item(i))
            End If
        Next

        For i = 1 To Request.Form.Count
            If LCase(Request.Form.Key(i)) <> "page" Then
                strQueryString = strQueryString & "&"
                strQueryString = strQueryString & Server.URLEncode(Request.Form.Key(i)) & "="
                strQueryString = strQueryString & Server.URLEncode(Request.Form.Item(i))
            End If
        Next

        If Len(strQueryString) <> 0 Then
            strQueryString = "?" & Mid(strQueryString, 2) & "&"
        Else
            strQueryString = "?"
        End If

        strScript = Request.ServerVariables("SCRIPT_NAME") & strQueryString
   
        If intPage <= 10 Then
            intStart = 1
        Else
            If (intPage Mod 10) = 0 Then
                intStart = intPage - 9
            Else
                intStart = intPage - (intPage Mod 10) + 1
            End If
        End If

        intEnd = intStart + 9
        If intEnd > intPageCount Then intEnd = intPageCount
   
        strRet = "Page " & intPage & " of " & intPageCount & ": "
   
        If intPage <> 1 Then
            strRet = strRet & "<a href=""" & strScript
            strRet = strRet & "page=" & intPage - 1
            strRet = strRet & """>&lt;&lt;Prev</a> "
        End If
   
        For i = intStart To intEnd
            If i = intPage Then
                strRet = strRet & "<b>" & i & "</b> "
            Else
                strRet = strRet & "<a href=""" & strScript
                strRet = strRet & "page=" & i
                strRet = strRet & """>" & i & "</a>"
                If i <> intEnd Then strRet = strRet & " "
            End If
        Next
   
        If intPage <> intPageCount Then
            strRet = strRet & " <a href=""" & strScript
            strRet = strRet & "page=" & intPage + 1
            strRet = strRet & """>Next&gt;&gt;</a> "
        End If
    End If
   
    Paging = strRet
End Function

%><html>
<head>
<title>www.34sale.net</title>
<link rel="stylesheet" type="text/css" href="style.css">


<STYLE type=text/css>
.child {
    DISPLAY: none
}
tr          { background-color: #EFEFEF }
</STYLE>

<SCRIPT language=JavaScript id=code>
<!--
  function swapDisplay() {   
    // Make sure a child element exists
    var child = event.srcElement.getAttribute("child");
    if (null!=child) {
      var el = document.all[child]
      if (null!=el)
        el.style.display = ""==el.style.display ? "block" : ""
    }
  }
  document.onclick = swapDisplay;
// -->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function go(loc) {
window.location.href = loc;
}
//  End -->
</script>

<STYLE type=text/javascript>
<!--
  classes.child.ALL.display = "block"
// -->
</STYLE>



</head>
<body bgcolor="#EFEFEF" onload="java script: document.frmSearch.id.select();<%

if Request.Querystring("alert") = "yes" then%>
alert('An email has been sent to you.\nTo see your advertisment here, you simply need to check your mail and follow it´s instructions.\n\n Thank you very much for your advertisement!')<% end if%>" link="#000000">
<p>&nbsp;</p>
  <center>
<TABLE cellPadding=3 bordercolor="#FFFFFF" style="border-collapse: collapse" cellspacing="1" border="0" bgcolor="#FFFFFF" width="720">
<form name="frmSearch" method="post" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<tr noWrap>
<td colspan="10" align="center" bgcolor="#EAEAEA">
<input type="hidden" name="page" value="1">
<input type="text" name="keyword" size="20" value="<%=Server.HTMLEncode(Request("keyword"))%>"><% 

dim RS, Conn

SQL = "SELECT * FROM Tblcategory "

Set Conn = Server.CreateObject("ADODB.Connection")

Conn.Open "DBQ=" & Server.Mappath("market.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, Conn

%>

<%If RS.EOF Then%><%Else%>
<select size="1" name="category">
<option value="">Select Category</option>
<%Do While Not RS.EOF%><option value="<%= RS("ID")%>" <% if Request("category")= ""&RS("ID")&""  then response.write "selected" end if%>><%= RS("Colcategory")%></option>
<% RS.MoveNext%><%Loop%></select><%end if

  RS.Close
  Set RS = Nothing

  SQL = "SELECT * FROM Tblinterest "

  Set RS = Server.CreateObject("ADODB.Recordset")
  RS.Open SQL, Conn

If RS.EOF Then%><%Else
%><select size="1" name="interest">
<option value="">Select Interest</option>
<%Do While Not RS.EOF%><option value="<%= RS("ID")%>" <%= RS("ID")%>" <% if Request("interest")= ""&RS("ID")&""  then response.write "selected" end if%>><%= RS("Description")%></option>
<%
RS.MoveNext
Loop
%></select><%

  end if

  RS.Close
  Set RS = Nothing

  SQL = "SELECT * FROM Tblstate "

  Set RS = Server.CreateObject("ADODB.Recordset")
  RS.Open SQL, Conn

If RS.EOF Then%><%Else%><select size="1" name="state">
<option value="">Select State</option>
<%Do While Not RS.EOF%><option value="<%= RS("ID")%>" <%= RS("ID")%>" <% if Request("state")= ""&RS("ID")&""  then response.write "selected" end if%>><%= RS("Colstate")%></option>
<%
RS.MoveNext
Loop
%></select><%end if%><%
  RS.Close
  Set RS = Nothing
  Conn.Close
  Set Conn = Nothing

%>
<input type="button" name="btnRestart" value="Clear" onclick="java script: window.location='<%=Request.ServerVariables("SCRIPT_NAME")%>'">
<input type="submit" name="btnSubmit" value="Search"><DIV class=parent CHILD="1a" style="CURSOR: hand; width:366; height:16">Advanced
search</DIV><DIV class=child id="1a" style="width: 801; height: 20">
<font size="1">Minimum Price:<input type="text" name="minprice" size="3" value="<%=Server.HTMLEncode(Request("minprice"))%>" style="font-size: 8pt">
Maximum: <input type="text" name="maxprice" size="2" value="<%=Server.HTMLEncode(Request("maxprice"))%>" style="font-size: 8pt"> Product ID:<input type="text" name="id" size="3" value="<%=Server.HTMLEncode(Request("id"))%>" style="font-size: 8pt">
Sort By:<select name="sortby" style="font-size: 8pt">
<option>Product ID</option>
<option value="text"<%If Trim(LCase(Request("sortby"))) = "text" Then Response.Write " selected"%>>Product Text</option>
<option value="title"<%If Trim(LCase(Request("sortby"))) = "title" Then Response.Write " selected"%>>Title</option>
<option value="price"<%If Trim(LCase(Request("sortby"))) = "price" Then Response.Write " selected"%>>Price</option>
</select>
<select name="sortorder" style="font-size: 8pt">
<option>Asc/ Desc</option>
<option value="asc">Ascending</option>
<option <%If Trim(LCase(Request("sortorder"))) = "desc" Then Response.Write " selected"%>>
Descending</option>
</select>
Records Per Page:<input type="text" name="pagesize" size="3" value="<%=intPageSize%>" style="font-size: 8pt">
Picture </font>
<input type="checkbox" name="pic" value="ON" <% if request("pic")="ON" then response.write "checked" end if %> style="background-color: #D4D4D4"></DIV></td>
</tr>
</form>
</table>

  </center>
</div>

<table align="center" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" height="23">
<tr>
<td><%If objRs.EOF Then%>No products found!<%Else%><!--Records Found-->Products Found: <%=objRs.RecordCount%></td>
</tr>
</table>

  <div align="center">
    <center>

<TABLE cellPadding=3 bordercolor="#FFFFFF" style="border-collapse: collapse" cellspacing="1" border="0" bgcolor="#FFFFFF" class="maintable">
<tr bgcolor="#C9C9C9">
<td align="center" style="background-color: #C9C9C9">Date <img border="0" src="time.gif"></td>
<% If (Request("pic")) = "" Then%><td style="background-color: #C9C9C9"><img border="0" src="cam.gif"></td><%end if%>
<td style="background-color: #C9C9C9">Title </td>
<% If (Request("interest")) = "" Then%><td align="center" style="background-color: #C9C9C9">I</td><%end if%>
<% If (Request("category")) = "" Then%><td style="background-color: #C9C9C9">Category</td><%end if%>
<% If (Request("state")) = "" Then%><td style="background-color: #C9C9C9">State</td><%end if%>
<td style="background-color: #C9C9C9">Price <img border="0" src="money.gif"></td>
<td style="background-color: #C9C9C9">
<img border="0" src="trash.gif" alt="You deside witch advertisements that are appropiate. Vote away those you don´t like by clicking the X."></td>
</tr><%

If objRs.PageCount < intPage Then intPage = objRs.PageCount
objRs.AbsolutePage = intPage
               
Do While Not objRs.EOF And intRecord <= intPageSize
                   
%>
<tr onClick="go('checkitem.asp?id=<%=objRs("Tbladd.ID").Value%>');" height="23" onMouseOver="this.style.backgroundColor='#FFCC66';" onMouseOut="this.style.backgroundColor='';">
<td><%= FormatDateTime(objRs("Date"))%>&nbsp;</td>
<% If (Request("pic")) = "" Then%><td align="center"><% if objRs("ContentType") <> "" then%>P<%elseif objRs("Picturelink") <> "" then%>P<%end if%>&nbsp;</td><%end if%>
<td><a href="checkitem.asp?ID=<%=objRs("Tbladd.ID")%>"><%=objRs("Title").Value%></a>&nbsp;</td>
<% If (Request("interest")) = "" Then%><td align="center"><%=objRs("Colinterest").Value%>&nbsp;</td><%end if%>
<% If (Request("category")) = "" Then%><td><%=objRs("Colcategory").Value%>&nbsp;</td><%end if%>
<% If (Request("state")) = "" Then%><td><%=objRs("Colstate").Value%>&nbsp;</td><%end if%>
<td align="right"><%=objRs("Price").Value%>&nbsp;</td>
<td align="center"><a href="http://computername/voteerase.asp?id=<%=objRs("Tbladd.ID")%>">X</a></font></td>
</tr><%

intRecord = intRecord + 1
objRs.MoveNext
Loop

%></table>

    </center>
</div>

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%">
<tr height="23">
<td align="center"><%=Paging(intPage, objRs.PageCount, objRs.RecordCount)%>&nbsp;</td>
</tr><%
End If%>
</table>
</center>
</div>
<p align="center">&nbsp;</p>
<p> </p>
</body></html><%
'Object cleanup
If IsObject(objRs) Then
    If Not objRs Is Nothing Then
        If objRs.State = adStateOpen Then objRs.Close
        Set objRs = Nothing
    End If
End If

If IsObject(objCn) Then
    If Not objCn Is Nothing Then
        If objCn.State = adStateOpen Then objCn.Close
        Set objCn = Nothing
    End If
End If
%>
Avatar billede rashid Praktikant
04. marts 2004 - 23:21 #4
hmm...
Måske var det en kende for meget kode.

Det korte af det lange er at jeg vil have en søgefunktion der kan hente data fra databasen (M$ Access) via en dynamisk SQL (i ASP).
SQL kan jeg godt formulere: der skal vælges udfra kategori, underkategori, type og pris. derefter vises data.
håber nogen kan hjælpe
Avatar billede rashid Praktikant
09. marts 2004 - 22:22 #5
hmm... lukker for nu.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester