運行環境:IIS 腳本語言:VBScript 數據庫:Access/SQL Server 數據庫語言:SQL 1.概要: 不論是在論壇,還是新聞系統,或是下載系統等動態網站中,大家經常會看到搜索功能:搜索帖子,搜索用戶,搜索軟件(總之搜索關鍵字)等,本文則是介紹如何建立一個高效實用的,基于ASP的站內多值搜索。 本文面對的是“多條件模糊匹配搜索”,理解了多條件的,單一條件搜索也不過小菜一碟了。一般來講,有兩種方法進行多條件搜索:枚舉法和遞進法。搜索條件不太多時(n<=3),可使用枚舉法,其語句頻度為2的n次方,成指數增長,n為條件數。很明顯,當條件增多以后,無論從程序的效率還是可實現性考慮都應采用遞進法,其語句頻度為n,成線性增長。需要指出的是,枚舉法思路非常簡單,一一判斷條件是否為空,再按非空條件搜索,同時可以利用真值表技術來對付條件極多的情況(相信沒人去干這種事,4條件時就已經要寫16組語句了);遞進法的思想方法較為巧妙,重在理解,其巧就巧在一是使用了標志位(flag),二是妙用SQL中字符串連接符&。下面以實例來講解引擎的建立。 2.實例: 我們建立一通訊錄查詢引擎,數據庫名為addressbook.mdb,表名為address,字段如下:
Web搜索界面如下:
采用枚舉法的源程序如下: <%@ CODEPAGE = "936" %> '連接數據庫 <% dim conn dim DBOath dim rs dim sql Set conn=Server.CreateObject("ADODB.Connection") DBPath = Server.MapPath("addressbook.mdb") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath Set rs=Server.CreateObject("ADODB.Recordset") '從Web頁獲取姓名、電話、學校的值 dim Name dim Tel dim School Name=request("Name") Tel=request("Tel") School=request("School") '枚舉法的搜索核心,因為有3個條件所以要寫8組If判斷語句 if trim(Name)="" and trim(Tel)="" and trim(School)="" then sql="select * from address order by ID asc" end if if trim(Name)="" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if rs.open sql,conn,1,1 '顯示搜索結果 if rs.eof and rs.bof then response.write "目前通訊錄中沒有記錄" else do while not rs.eof response.write "姓名:"&rs("Name")&"電話:"&rs("Tel")&"學校:"&rs("School")&"<br>" rs.movenext loop end if '斷開數據庫 set rs=nothing conn.close set conn=nothing %> 理解上述程序時,著重琢磨核心部分,8組語句一一對應了3個搜索框中的8種狀態
另外trim()是VB的函數,將輸入的字符串前后的空格去掉;%是SQL語言中的多字符通配符(_是單字符通配符),由此可見%"&trim()&"%對搜索框中輸入的關鍵字是分別向左向右匹配的;SQL語言中用and連接說明非空條件之間是“與”關系。 再來看看遞進法,與枚舉法相比它們只有核心部分不同: '遞進法的搜索核心,依次判斷條件為空否,非空則將其加入搜索條件 sql="select * from address where" if Name<>"" then sql=sql&" Name like '%"&Name&"%' " flag=1 end if if Tel<>"" and flag=1 then sql=sql&" and Tel like '%"&Tel&"%'" flag=1 elseif Tel<>"" then sql=sql&" Tel like '%"&Tel&"%'" flag=1 end if if Company<>"" and flag=1 then sql=sql&" and Company like '%"&Company&"%'" flag=1 elseif Company <>"" then sql=sql&" Company like '%"&Company&"%'" flag=1 end if if flag=0 then sql="select * from address order by ID asc" end if rs.open sql,conn,1,1 遞進法是一個明智的算法,單從語句的長短就可以看出來了。這個算法的難點和精髓就在flag和&上。首先你應該清楚&在SQL中就是一個字符串連接符,把該符號左右的字符拼接在一起。再回到程序,當Name不為空時sql="select * from address where Name like '%"&Name&"%' "同時flag=1;接下來當Name不為空時且Tel不為空時,即Tel<>"" and flag=1時,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同時flag=1,否則當Name為空Tel不為空,sql="select * from address where Tel like '%"&Tel&"%' "同時flag=1;以此類推就可以推廣到n個條件的搜索。當然條件皆為空時,即flag=0將選擇所有表中所有項。 3.驗證: 至此,一個搜索引擎就建立起來了。以下是一些使用示例:
搜索結果為: 姓名: 張三 電話:33333333 單位:電子科技大學計算機系
搜索結果為: 姓名:張三 電話:33333333 單位:電子科技大學計算機系 姓名 李 四 電話:44444444 單位:四川大學生物系 姓名:王二 電話:22222222 單位:西南交通大學建筑系
搜索結果為: 姓名 李 四 電話:44444444 單位:四川大學生物系
搜索結果為: 姓名:王二 電話:22222222 單位:西南交通大學建筑系 4.改進: 其實這個引擎還有些缺陷,問題主要在于通配符%。一方面是因為人們平時習慣把*作為通配符,另一方面%若出現在超鏈接中,通過request獲取時%將被“吃”掉,如下: --test.htm-- … <a href=test.asp?content=test%the%sign>click here</a> … --test.asp-- <% content=request(“content”) response.write content %> 在IE中瀏覽test.htm時點擊超鏈接,顯示為: testthesign 可見%直接被超鏈接忽略掉了。怎么才能解決這個問題呢?很簡單,我們做點小小的手腳--偷梁換柱。 將以下代碼加在搜索核心之前: Name=replace(Name,"*","%") Tel=replace(Tel,"*","%") Company=replace(Company,"*","%") 將以下代碼加在搜索核心之后: Name=replace(Name,"%","*") Tel=replace(Tel,"%","*") Company=replace(Company,"%","*") 在我們來分析一下這些語句。replace()是VB中字符串替換函數,replace(Name,"*","%") 就是將Name中所有的*換成%。也就是說,我們把3個條件中凡是出現的*都替換為%,這樣一來前3句就將通配符改成*了。而后3句就可以防止%被“吃”掉。所有問題就迎刃而解了吧。
搜索結果為: 姓名:王 二 電話:22222222 單位:西南交通大學建筑系 將上面的語句再改一改,把*用空格代替,不就成了我們在Google、BaiDu中常用的用空格來分開搜索條件的搜索引擎了嗎? 運行環境:IIS 腳本語言:VBScript 數據庫:Access/SQL Server 數據庫語言:SQL 1.概要: 不論是在論壇,還是新聞系統,或是下載系統等動態網站中,大家經常會看到搜索功能:搜索帖子,搜索用戶,搜索軟件(總之搜索關鍵字)等,本文則是介紹如何建立一個高效實用的,基于ASP的站內多值搜索。 本文面對的是“多條件模糊匹配搜索”,理解了多條件的,單一條件搜索也不過小菜一碟了。一般來講,有兩種方法進行多條件搜索:枚舉法和遞進法。搜索條件不太多時(n<=3),可使用枚舉法,其語句頻度為2的n次方,成指數增長,n為條件數。很明顯,當條件增多以后,無論從程序的效率還是可實現性考慮都應采用遞進法,其語句頻度為n,成線性增長。需要指出的是,枚舉法思路非常簡單,一一判斷條件是否為空,再按非空條件搜索,同時可以利用真值表技術來對付條件極多的情況(相信沒人去干這種事,4條件時就已經要寫16組語句了);遞進法的思想方法較為巧妙,重在理解,其巧就巧在一是使用了標志位(flag),二是妙用SQL中字符串連接符&。下面以實例來講解引擎的建立。 2.實例: 我們建立一通訊錄查詢引擎,數據庫名為addressbook.mdb,表名為address,字段如下:
Web搜索界面如下:
采用枚舉法的源程序如下: <%@ CODEPAGE = "936" %> '連接數據庫 <% dim conn dim DBOath dim rs dim sql Set conn=Server.CreateObject("ADODB.Connection") DBPath = Server.MapPath("addressbook.mdb") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath Set rs=Server.CreateObject("ADODB.Recordset") '從Web頁獲取姓名、電話、學校的值 dim Name dim Tel dim School Name=request("Name") Tel=request("Tel") School=request("School") '枚舉法的搜索核心,因為有3個條件所以要寫8組If判斷語句 if trim(Name)="" and trim(Tel)="" and trim(School)="" then sql="select * from address order by ID asc" end if if trim(Name)="" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)="" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)="" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)="" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" end if if trim(Name)<>"" and trim(Tel)<>"" and trim(School)<>"" then sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" end if rs.open sql,conn,1,1 '顯示搜索結果 if rs.eof and rs.bof then response.write "目前通訊錄中沒有記錄" else do while not rs.eof response.write "姓名:"&rs("Name")&"電話:"&rs("Tel")&"學校:"&rs("School")&"<br>" rs.movenext loop end if '斷開數據庫 set rs=nothing conn.close set conn=nothing %> 理解上述程序時,著重琢磨核心部分,8組語句一一對應了3個搜索框中的8種狀態
另外trim()是VB的函數,將輸入的字符串前后的空格去掉;%是SQL語言中的多字符通配符(_是單字符通配符),由此可見%"&trim()&"%對搜索框中輸入的關鍵字是分別向左向右匹配的;SQL語言中用and連接說明非空條件之間是“與”關系。 再來看看遞進法,與枚舉法相比它們只有核心部分不同: '遞進法的搜索核心,依次判斷條件為空否,非空則將其加入搜索條件 sql="select * from address where" if Name<>"" then sql=sql&" Name like '%"&Name&"%' " flag=1 end if if Tel<>"" and flag=1 then sql=sql&" and Tel like '%"&Tel&"%'" flag=1 elseif Tel<>"" then sql=sql&" Tel like '%"&Tel&"%'" flag=1 end if if Company<>"" and flag=1 then sql=sql&" and Company like '%"&Company&"%'" flag=1 elseif Company <>"" then sql=sql&" Company like '%"&Company&"%'" flag=1 end if if flag=0 then sql="select * from address order by ID asc" end if rs.open sql,conn,1,1 遞進法是一個明智的算法,單從語句的長短就可以看出來了。這個算法的難點和精髓就在flag和&上。首先你應該清楚&在SQL中就是一個字符串連接符,把該符號左右的字符拼接在一起。再回到程序,當Name不為空時sql="select * from address where Name like '%"&Name&"%' "同時flag=1;接下來當Name不為空時且Tel不為空時,即Tel<>"" and flag=1時,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同時flag=1,否則當Name為空Tel不為空,sql="select * from address where Tel like '%"&Tel&"%' "同時flag=1;以此類推就可以推廣到n個條件的搜索。當然條件皆為空時,即flag=0將選擇所有表中所有項。 3.驗證: 至此,一個搜索引擎就建立起來了。以下是一些使用示例:
搜索結果為: 姓名: 張三 電話:33333333 單位:電子科技大學計算機系
搜索結果為: 姓名:張三 電話:33333333 單位:電子科技大學計算機系 姓名 李 四 電話:44444444 單位:四川大學生物系 姓名:王二 電話:22222222 單位:西南交通大學建筑系
搜索結果為: 姓名 李 四 電話:44444444 單位:四川大學生物系
搜索結果為: 姓名:王二 電話:22222222 單位:西南交通大學建筑系 4.改進: 其實這個引擎還有些缺陷,問題主要在于通配符%。一方面是因為人們平時習慣把*作為通配符,另一方面%若出現在超鏈接中,通過request獲取時%將被“吃”掉,如下: --test.htm-- … <a href=test.asp?content=test%the%sign>click here</a> … --test.asp-- <% content=request(“content”) response.write content %> 在IE中瀏覽test.htm時點擊超鏈接,顯示為: testthesign 可見%直接被超鏈接忽略掉了。怎么才能解決這個問題呢?很簡單,我們做點小小的手腳--偷梁換柱。 將以下代碼加在搜索核心之前: Name=replace(Name,"*","%") Tel=replace(Tel,"*","%") Company=replace(Company,"*","%") 將以下代碼加在搜索核心之后: Name=replace(Name,"%","*") Tel=replace(Tel,"%","*") Company=replace(Company,"%","*") 在我們來分析一下這些語句。replace()是VB中字符串替換函數,replace(Name,"*","%") 就是將Name中所有的*換成%。也就是說,我們把3個條件中凡是出現的*都替換為%,這樣一來前3句就將通配符改成*了。而后3句就可以防止%被“吃”掉。所有問題就迎刃而解了吧。
搜索結果為: 姓名:王 二 電話:22222222 單位:西南交通大學建筑系 將上面的語句再改一改,把*用空格代替,不就成了我們在Google、BaiDu中常用的用空格來分開搜索條件的搜索引擎了嗎? |
溫馨提示:喜歡本站的話,請收藏一下本站!