ASP.net搜索功能编辑

时间:2014-06-13 08:57:13

标签: asp.net sql sql-like

我被要求在别人的代码中编辑搜索功能。

我不会掌握这种特殊语言的大量知识,虽然我确信它很容易解决,但我不想在没有建议的情况下打破它。

搜索功能带来了太多的结果,搜索部分的单词。因此,搜索" e"会带来一些" e"在它..

我需要编辑以搜索完整的单词。

这是整个代码。

    <!-- #include virtual="/site-elements/includes/config.asp" -->
<!-- #include virtual="/site-elements/includes/folder-string-functions.asp" -->
<!-- #include virtual="/site-elements/includes/database-open.asp" -->
<!-- #include virtual="/site-elements/includes/field-filtering-functions.asp" -->
<!-- #include virtual="/site-elements/includes/login-checker.asp" -->
<!-- #include virtual="/site-elements/includes/date-functions.asp" -->
<!-- #include virtual="/site-elements/includes/form-counter.asp" -->
<!-- #include virtual="/site-elements/includes/email-check-code-functions.asp" -->
<!-- #include virtual="/site-elements/includes/trimstring-functions.asp" -->
<!-- #include virtual="/site-elements/includes/toilehighlighter-functions.asp" -->
<!-- #include virtual="/site-elements/includes/paging-functions.asp" -->
<!-- #include virtual="/site-elements/includes/regular-expression-functions.asp" -->
<%
lightboxgallery = "Yes"

searchfor = fieldfiltering(Request("searchfor"))
searchfor = TRIM(searchfor)
IF RIGHT(searchfor, 1) = "/" then searchfor = LEFT(searchfor, LEN(searchfor)-1)

seotitle = "Tube Tech International - search results for: " & searchfor
seodescription = "Tube Tech International search results for: " & searchfor
seokeywords = "Tube Tech International, " & searchfor

Session("searchedfor") = searchfor
Session("searchedfortime") = now()
%>

<!-- #include virtual="/site-elements/template/template-start.asp" -->

<%

'#### Paging ####
thispage = id1
If NOT ISNUMERIC(thispage) OR thispage = "" then 
thispage = 1
Else
thispage = CINT(thispage)
End If
'#################

%>

<div id="breadcrumbs">
<form action="/search/" method="get" class="searchform"><input type="submit" name="submit" tabindex="2" value="Search" class="search-click" /><input type="text" tabindex="1" value="Type your search here" onfocus="this.value=''" name="searchfor" class="search-box" /></form>
<a href="/"><img src="/images/home.png" alt="Home" width="12" height="12" class="home" border="0" /></a>
<a href="/" class="active">Home</a>
<a href="#">Search for: <i><% =searchfor %></i></a>
</div> 

<div id="searchcontent">

    <div class="leftbox">
    <h1>Search</h1>
    <p class="opening"><b>Your search results: <% =searchfor %></b></p>
    <div class="clear"></div>
    </div>

   <% If searchfor <> "" then %>    

   <div class="rightbox">
        <h2>Photo Gallery</h2>
        <p>Click the images for a larger view.</p>

        <div id="gallery">

            <%

            listingstringloop = ""
            listingstring = ""
            counterdiv = 0

            strSQL = ""

            strSQL        = "SELECT 'case-studies' AS pagetype, cs.title AS entrytitle, cs.casestudyid AS id, csi.casestudyimageid AS imageid, csi.imagetitle,  LIKE (cs.title, cs.content) AGAINST ('" & searchfor & "') AS relevance, LIKE (csi.imagetitle) AGAINST ('" & searchfor & "') AS imagerelevance "

            strSQL = strSQL & "FROM casestudies AS cs, casestudiesimages AS csi "

            strSQL = strSQL & "WHERE cs.casestudyid = csi.casestudyid AND cs.modestatus = '1' AND ((LIKE(csi.imagetitle) OR (cs.title like '%"& searchfor & "%') OR (cs.content like '%" & searchfor & "%') OR (csi.imagetitle like '%" & searchfor & "%')) "

            strSQL = strSQL & "UNION ALL "


            strSQL = strSQL & "SELECT 'news' AS pagetype, news.title AS entrytitle, news.newsid AS id, newsimages.newsimageid AS imageid, newsimages.imagetitle, LIKE (news.title, news.content) AGAINST ('" & searchfor & "') AS relevance, LIKE (newsimages.imagetitle) AGAINST ('" & searchfor & "') AS imagerelevance "

            strSQL = strSQL & "FROM news, newsimages "

            strSQL = strSQL & "WHERE news.newsid = newsimages.newsid AND news.modestatus = '1' AND ((news.title like '%" & searchfor & "%') OR (news.content like '%" & searchfor & "%') OR (newsimages.imagetitle like '%" & searchfor & "%')) "

            strSQL = strSQL & "GROUP BY id "

            strSQL = strSQL & "ORDER BY relevance DESC, imagerelevance DESC "


            Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then

            counter = 1

               Do while NOT rsSearch.eof 

                If counter > 2 then counter = 1       

                thisid = reversefieldfiltering(rsSearch("id"))
                thistitle = reversefieldfiltering(rsSearch("entrytitle"))
                imageid = reversefieldfiltering(rsSearch("imageid"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                imagetitle = reversefieldfiltering(rsSearch("imagetitle"))

                listingstring = "<a href=""/images/" & pagetype & "/images/" & imageid & "-3.jpg"" class=""normal"" title=""" & imagetitle

                If pagetype = "case-studies" then
                listingstring = listingstring & "<br />To read the case study in full please <a href='http://www.tubetech.com" & buildlink("/case-studies/" & thisid & "_" & thistitle & "/") & "'>click here</a>"
                ElseIf pagetype = "news" then
                listingstring = listingstring & "<br />To read the news story in full please <a href='http://www.tubetech.com" & buildlink("/news/" & thisid & "_" & thistitle & "/") & "'>click here</a>"
                End If

                listingstring = listingstring & """>"

                listingstring = listingstring & "<img src=""/images/" & pagetype & "/transparent.gif"" style=""background-image:url('/images/" & pagetype & "/images/" & imageid & "-1.jpg');background-position:center;"" width=""115"" height=""86"" alt=""" & imagetitle & """ title=""" & imagetitle & """ "
                If counter = 1 then 
                    listingstring = listingstring & "class=""left"""
                Else 
                    listingstring = listingstring & "class=""right"""
                End If 
                listingstring = listingstring & "/></a>" & vbcrlf

                listingstringloop = listingstringloop & listingstring

            counter = counter + 1
            counterdiv= counterdiv + 1    
            rsSearch.MoveNext
            loop        
            %>

                <div class="photocontainer"<% If counterdiv < 7 then %> style="height:auto;"<% End If %>>
                <div id="photocontent" style="overflow: hidden;">

                <% =listingstringloop %>

                </div>
                </div>

            <%          
            End if 

            rsSearch.Close
            Set rsSearch = Nothing

            %>              

        </div>
    </div>

    <div class="leftbox">

        <% If searchfor <> "" then %>


            <%

            searchrecordin = "No"

            listingstringloop = ""
            listingstring = ""
            counterleftbox = 0

            strSQL = ""
            strSQL  = "SELECT 'case-studies' AS pagetype, cs.casestudyid AS id, cs.title, cs.content, 0 AS menutype, '' AS menuoption, LIKE (cs.title, cs.content) AGAINST ('" & searchfor & "') AS relevance FROM casestudies AS cs "          
            strSQL = strSQL & "WHERE cs.modestatus = '1' AND (LIKE(cs.title, cs.content) AGAINST ('" & searchfor & "' IN BOOLEAN MODE) OR (cs.title like '%" & searchfor & "%') OR (cs.content like '%" & searchfor & "%')) "           
            strSQL = strSQL & "GROUP BY id "
            strSQL = strSQL & "ORDER BY lastupdated DESC "
'           strSQL = strSQL & "ORDER BY relevance DESC "            
            Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then
            searchrecordin = "Yes"
            %>
            <h2 class="section">Case studies</h2>
            <%          
                Do while NOT rsSearch.eof            

                id = reversefieldfiltering(rsSearch("id"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                title = reversefieldfiltering(rsSearch("title"))
                menutype = reversefieldfiltering(rsSearch("menutype"))
                menuoption = reversefieldfiltering(rsSearch("menuoption"))

                content = rsSearch("content")
                content = reversefieldfiltering(content)
                content = striptags(content)
                content = trimstring(content, 148)
                content = toilehighlighter(content, searchfor)
                content = content & "..."

                If pagetype = "web-pages" then
                    If menutype = "1" then              
                        linkname = buildlink("/" & title & "/")             
                    ElseIf menutype = "2" then
                        linkname = buildlink("/" & menuoption & "/" & id & "_" & title & "/")               
                    End If  
                Else

                    linkname = buildlink("/" & pagetype & "/" & id & "_" & title & "/") 

                End If

                title = toilehighlighter(title, searchfor)

                listingstring = "<h3><a href=""" & linkname & """>" & title & "</a></h3>" & vbcrlf
                listingstring = listingstring & "<p class=""opening""><a href=""" & linkname & """>" & content & "</a></p>" & vbcrlf

                listingstringloop = listingstringloop & listingstring


            counterleftbox = counterleftbox + 1
            rsSearch.MoveNext
            loop
            %>

            <div class="casestudiescontainer"<% If counterleftbox < 5 then %> style="height:auto;"<% End If %>>
            <div id="casestudiescontent">
            <% =listingstringloop %>        
            </div>
            </div>

            <% End if 

            rsSearch.Close
            Set rsSearch = Nothing  
            %>


            <%

            listingstringloop = ""
            listingstring = ""
            counterleftbox = 0

    strSQL = ""
                strSQL = "SELECT 'news' AS pagetype, news.newsid AS id, news.title, news.content, 0 AS menutype, '' AS menuoption, LIKE (news.title, news.content) AGAINST ('" & searchfor & "') AS relevance FROM news "
    strSQL = strSQL & "WHERE news.modestatus = '1' AND (LIKE(news.title, news.content) OR (news.title like '%" & searchfor & "%') OR (news.content like '%" & searchfor & "%')) "           
    strSQL = strSQL & "GROUP BY id "
    strSQL = strSQL & "ORDER BY relevance DESC "
                Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then
            searchrecordin = "Yes"
            %>          
            <h2 class="section">News</h2>            
            <%
                Do while NOT rsSearch.eof 

                id = reversefieldfiltering(rsSearch("id"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                title = reversefieldfiltering(rsSearch("title"))
                menutype = reversefieldfiltering(rsSearch("menutype"))
                menuoption = reversefieldfiltering(rsSearch("menuoption"))

                content = rsSearch("content")
                content = reversefieldfiltering(content)
                content = striptags(content)
                content = trimstring(content, 148)
                content = toilehighlighter(content, searchfor)
                content = content & "..."

                If pagetype = "web-pages" then
                    If menutype = "1" then              
                        linkname = buildlink("/" & title & "/")             
                    ElseIf menutype = "2" then
                        linkname = buildlink("/" & menuoption & "/" & id & "_" & title & "/")               
                    End If  
                Else

                    linkname = buildlink("/" & pagetype & "/" & id & "_" & title & "/") 

                End If              

                title = toilehighlighter(title, searchfor)

                listingstring = "<h3><a href=""" & linkname & """>" & title & "</a></h3>" & vbcrlf
                listingstring = listingstring & "<p class=""opening""><a href=""" & linkname & """>" & content & "</a></p>" & vbcrlf

                listingstringloop = listingstringloop & listingstring

            counterleftbox = counterleftbox + 1
            rsSearch.MoveNext
            loop

            %>

            <div class="newscontainer"<% If counterleftbox < 5 then %> style="height:auto;"<% End If %>>
            <div id="newscontent">  
            <% =listingstringloop %>        
            </div>
            </div>

            <% End if 

            rsSearch.Close
            Set rsSearch = Nothing  
            %>

            <%

            listingstringloop = ""
            listingstring = ""
            counterleftbox = 0

            strSQL = ""
            strSQL = "SELECT 'techniques' AS pagetype, tech.techniqueid AS id, tech.title, tech.content, 0 AS menutype, '' AS menuoption, LIKE (tech.title, tech.content) AGAINST ('" & searchfor & "') AS relevance FROM techniques AS tech "
            strSQL = strSQL & "WHERE tech.modestatus = '1' AND (LIKE(tech.title, tech.content) OR (tech.title like '%" & searchfor & "%') OR (tech.content like '%" & searchfor & "%')) "           
            strSQL = strSQL & "GROUP BY id "
            strSQL = strSQL & "ORDER BY relevance DESC "
            Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then            
            searchrecordin = "Yes"
            %>          
            <h2 class="section">Techniques</h2>               
            <%

                Do while NOT rsSearch.eof            

                id = reversefieldfiltering(rsSearch("id"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                title = reversefieldfiltering(rsSearch("title"))
                menutype = reversefieldfiltering(rsSearch("menutype"))
                menuoption = reversefieldfiltering(rsSearch("menuoption"))

                content = rsSearch("content")
                content = reversefieldfiltering(content)
                content = striptags(content)
                content = trimstring(content, 148)
                content = toilehighlighter(content, searchfor)
                content = content & "..."

                If pagetype = "web-pages" then
                    If menutype = "1" then              
                        linkname = buildlink("/" & title & "/")             
                    ElseIf menutype = "2" then
                        linkname = buildlink("/" & menuoption & "/" & id & "_" & title & "/")               
                    End If  
                Else

                    linkname = buildlink("/" & pagetype & "/" & id & "_" & title & "/") 

                End If              

                title = toilehighlighter(title, searchfor)

                listingstring = "<h3><a href=""" & linkname & """>" & title & "</a></h3>" & vbcrlf
                listingstring = listingstring & "<p class=""opening""><a href=""" & linkname & """>" & content & "</a></p>" & vbcrlf

                listingstringloop = listingstringloop & listingstring   

            counterleftbox = counterleftbox + 1                 
            rsSearch.MoveNext
            loop
            %>

            <div class="techniquescontainer"<% If counterleftbox < 5 then %> style="height:auto;"<% End If %>>
            <div id="techniquescontent">
            <% =listingstringloop %>        
            </div>
            </div>

            <% End if 

            rsSearch.Close
            Set rsSearch = Nothing  
            %>


            <%

            listingstringloop = ""
            listingstring = ""
            counterleftbox = 0

            strSQL = ""
            strSQL = "SELECT 'web-pages' AS pagetype, pages.pageid AS id, pages.title, pages.content, pages.menutype, pages.menuoption, LIKE (pages.title, pages.content) AGAINST ('" & searchfor & "') AS relevance FROM webpages AS pages "
            strSQL = strSQL & "WHERE pages.modestatus = '1' AND (LIKE(pages.title, pages.content) OR (pages.title like '%" & searchfor & "%') OR (pages.content like '%" & searchfor & "%')) "          
            strSQL = strSQL & "GROUP BY id "
            strSQL = strSQL & "ORDER BY relevance DESC "
            Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then
            searchrecordin = "Yes"          
            %>          
            <h2 class="section">Web Pages</h2>               
            <%
                Do while NOT rsSearch.eof            

                id = reversefieldfiltering(rsSearch("id"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                title = reversefieldfiltering(rsSearch("title"))
                menutype = reversefieldfiltering(rsSearch("menutype"))
                menuoption = reversefieldfiltering(rsSearch("menuoption"))

                content = rsSearch("content")
                content = reversefieldfiltering(content)
                content = striptags(content)
                content = trimstring(content, 148)
                content = toilehighlighter(content, searchfor)
                content = content & "..."

                If pagetype = "web-pages" then
                    If menutype = "1" then              
                        linkname = buildlink("/" & title & "/")             
                    ElseIf menutype = "2" then
                        linkname = buildlink("/" & menuoption & "/" & id & "_" & title & "/")               
                    End If  
                Else

                    linkname = buildlink("/" & pagetype & "/" & id & "_" & title & "/") 

                End If              

                title = toilehighlighter(title, searchfor)

                listingstring = "   <h3><a href=""" & linkname & """>" & title & "</a></h3>" & vbcrlf
                listingstring = listingstring & "   <p class=""opening""><a href=""" & linkname & """>" & content & "</a></p>" & vbcrlf

                listingstringloop = listingstringloop & listingstring   

            counterleftbox = counterleftbox + 1         
            rsSearch.MoveNext
            loop
            %>

            <div class="webpagescontainer"<% If counterleftbox < 5 then %> style="height:auto;"<% End If %>>
            <div id="webpagescontent">
             <% =listingstringloop %>               
            </div>
            </div>

            <% End if 

            rsSearch.Close
            Set rsSearch = Nothing  
            %>


            <%

            listingstringloop = ""
            listingstring = ""
            counterleftbox = 0

            strSQL = ""
            strSQL = "SELECT 'categories' AS pagetype, cat.categoryid AS id, cat.category AS title, cat.content, 0 AS menutype, '' AS menuoption, LIKE (cat.category, cat.content) AGAINST ('" & searchfor & "') AS relevance FROM categories AS cat "
            strSQL = strSQL & "WHERE (LIKE(cat.category, cat.content) AGAINST ('" & searchfor & "' IN BOOLEAN MODE) OR (cat.category like '%" & searchfor & "%') OR (cat.content like '%" & searchfor & "%')) "                     
            strSQL = strSQL & "GROUP BY id "
            strSQL = strSQL & "ORDER BY relevance DESC "
            Set rsSearch = my_conn.Execute(strSQL)

            If rsSearch.EOF = False then
            searchrecordin = "Yes"          
            %>          
            <h2 class="section">Categories</h2>                  
            <%
                Do while NOT rsSearch.eof            

                id = reversefieldfiltering(rsSearch("id"))
                pagetype = reversefieldfiltering(rsSearch("pagetype"))
                title = reversefieldfiltering(rsSearch("title"))
                menutype = reversefieldfiltering(rsSearch("menutype"))
                menuoption = reversefieldfiltering(rsSearch("menuoption"))

                content = rsSearch("content")
                content = reversefieldfiltering(content)
                content = striptags(content)
                content = trimstring(content, 148)
                content = toilehighlighter(content, searchfor)
                content = content & "..."

                If pagetype = "web-pages" then
                    If menutype = "1" then              
                        linkname = buildlink("/" & title & "/")             
                    ElseIf menutype = "2" then
                        linkname = buildlink("/" & menuoption & "/" & id & "_" & title & "/")               
                    End If  
                Else

                    linkname = buildlink("/" & pagetype & "/" & id & "_" & title & "/") 

                End If

                title = toilehighlighter(title, searchfor)

                listingstring = "   <h3><a href=""" & linkname & """>" & title & "</a></h3>" & vbcrlf
                listingstring = listingstring & "   <p class=""opening""><a href=""" & linkname & """>" & content & "</a></p>" & vbcrlf

                listingstringloop = listingstringloop & listingstring   

            counterleftbox = counterleftbox + 1         
            rsSearch.MoveNext
            loop
            %>

            <div class="categoriescontainer"<% If counterleftbox < 5 then %> style="height:auto;"<% End If %>>
            <div id="categoriescontent" style="width: auto;">
             <% =listingstringloop %>                   
            </div>
            </div>

            <% End if 

            rsSearch.Close
            Set rsSearch = Nothing  


            If searchrecordin = "No" then
            %>
                <p class="opening"><b>We are sorry, but no matches were found matching your search term. Please try a different search term.</b></p>
            <%
            End If

        End If %>

    <div class="clear"></div>
    </div>


    <div class="rightbox">
        <h2>Video Gallery</h2>    

        <%  

        listingstringloop = ""
        listingstring = ""
        counterdiv = 0

        strSQL = ""
        strSQL  = "SELECT 'video-gallery' AS pagetype, vg.videogalleryid AS id, vg.title, vg.videocontent, vg.folderlink, LIKE (vg.title, vg.description) AGAINST ('" & searchfor & "') AS relevance FROM videogallery AS vg "          
        strSQL = strSQL & "WHERE (LIKE(vg.title, vg.description) AGAINST ('" & searchfor & "' IN BOOLEAN MODE) OR (vg.title like '%" & searchfor & "%') OR (vg.description like '%" & searchfor & "%')) "           
        strSQL = strSQL & "GROUP BY id "
        strSQL = strSQL & "ORDER BY relevance DESC "            
        Set rsSearch = my_conn.Execute(strSQL)

        If rsSearch.EOF = False then            

        imagecounter = 1

            Do while NOT rsSearch.eof 

            If imagecounter > 2 then imagecounter = 1

            youtubevideoid = ""

            id = reversefieldfiltering(rsSearch("id"))
            pagetype = reversefieldfiltering(rsSearch("pagetype"))
            videotitle = reversefieldfiltering(rsSearch("title"))
            folderlink = reversefieldfiltering(rsSearch("folderlink"))      
            videocontent = rsSearch("videocontent")
            videocontent = reversefieldfiltering(videocontent)

            regExpVideocontent = Replace(videocontent, "v/","#v=")      
            youtubevideoid = GetFirstMatch("[\\#&]v=([^?#]*)",regExpVideocontent)

            If youtubevideoid = "" then

                regExpVideocontent = GetFirstMatch("<iframe [^>]*src=""(.*?)""[^>]*>",videocontent)
                regExpVideocontent = Replace(regExpVideocontent,"http://www.youtube.com/embed/","")
                regExpVideocontent = Replace(regExpVideocontent,"?rel=0","")
                youtubevideoid = TRIM(regExpVideocontent)
                youtubevideoid = RTRIM(youtubevideoid)
                youtubevideoid = LTRIM(youtubevideoid)

            End If      


            listingstring = "   <a href=""/video-gallery/?a=1&vgid=" & id & """>"
            listingstring = listingstring & "<img src=""/images/transparent.gif"" style=""background-image:url('http://img.youtube.com/vi/" & youtubevideoid & "/1.jpg');background-position:center;"" border=""0"" width=""115"" height=""86"" alt=""" & videotitle & """ title=""" & videotitle & """ "
            If imagecounter = 1 then 
            listingstring = listingstring & "class=""left"""
            Else 
            listingstring = listingstring & "class=""right"""
            End If
            listingstring = listingstring & "/></a>" & vbcrlf

            listingstringloop = listingstringloop & listingstring       

            imagecounter = imagecounter + 1
            counterdiv = counterdiv + 1 
            rsSearch.MoveNext
            loop

          %>

            <div class="videocontainer"<% If counterdiv < 7 then %> style="height:auto;"<% End If %>>
            <div id="videocontent">                
            <% =listingstringloop %>
            </div>
            </div>

          <%
          End if 

          rsSearch.Close
          Set rsSearch = Nothing        
          %>   

    </div>

    <% End If %> 

<div class="clear"></div>
</div>


<!-- #include virtual="/site-elements/template/template-end.asp" -->
<!-- #include virtual="/site-elements/includes/database-close.asp" -->

通常我使用%来标记我的部分和完整的单词。所以无法解决他设置的问题......

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

如果您想搜索一个单词,那么您可以使用以下想法。

在构造SQL

之前调用以下行
searchfor = "[^a-zA-Z]" + searchfor + "[^a-zA-Z]"

这里的想法是搜索输入的内容,在该内容之前和之后没有字母。例如,如果您输入'at',那么它匹配'car at usa'而不是'cat'

这不是一个完美的解决方案,但只是一个想法,这可以扩展到排除更多的charectors