少し前に、私は複数フィールドのあいまい検索関数 (baidui に似た) を書きました。関数コードは次のとおりです
'================================================ = =
'関数名:keyword_souuo
'機能: SQL クエリ条件を生成します。
'パラメータ: table_field ------ テーブルのフィールド名 (カンマ区切り)
' キーワード ------ 検索キーワード(スペース区切り)
'戻り値: SQLクエリ条件
'================================================ = =
関数キーワード_sousuo(byvalテーブルフィールド,byvalキーワード)
dim str01,str02,キーワード,テーブルフィールド,i,j
table_fields=split(trim(table_field),,)
キーワード=split(トリム(キーワード),,)
if table_field<> then
str01=(&table_fields(0)& like '%&keyword&%'
j=0 から ubound(table_fields) まで
str01=str01& または &table_fields(j)& のように '%&keyword&%'
次
str01=str01&)
それ以外
response.Write(<script>alert('パラメータ エラー (空にすることはできません)!')</script>)
応答.End()
終了する場合
'全角→半角スペース
'キーワード=置換(キーワード, , )
'while InStr(キーワード, )>0
' キーワード=置換(キーワード, , )
'ウェン
キーワード=分割(キーワード, )
ubound(キーワード)>0の場合
for i=0 から ubound(キーワード)
str02=str02&(&table_fields(0)& like '%&keywords(i)&%'
j=1 から ubound(table_fields) まで
str02=str02& または &table_fields(j)& のように '%&keywords(i)&%'
次
str02=str02&)
次
str02=(&replace(str02,)(,)and()&)
キーワード_ソウスオ=( & str01 & または & str02 & )
それ以外
キーワード_ソウスオ=str01
終了する場合
終了関数
'================================================ = =
'関数名:keyword_tag
'機能: 文字列内のキーワードを赤色でマークします。
'パラメータ: str ------ 文字列
' キーワード ------- キーワードをマークします (スペースで区切ります)
'戻り値: 文字列(html形式)
'================================================ = =
関数キーワードタグ(byval str,byval キーワード)
dim キーワード、str01、str02、i
'全角→半角スペース
'キーワード=置換(キーワード, , )
'while InStr(キーワード, )>0
' キーワード=置換(キーワード, , )
'ウェン
str01=replace(str,キーワード,<font color=#ff0000>&キーワード&</font>)
キーワード=分割(キーワード, )
ubound(キーワード)>0の場合
str02=str
for i=0 から ubound(キーワード)
str02=replace(str02,キーワード(i),<font color=#ff0000>&キーワード(i)&</font>)
次
キーワードタグ=str02
それ以外
キーワードタグ=str01
終了する場合
終了関数
以下は例です http://www.zhouhaihe.com/blog/uploads/200604/recordread.asp
<%@LANGUAGE=VBSCRIPT コードページ=936%>
<%
明示的なオプション
ディム conn、connstr、データベース
connstr=DBQ=+server.mappath(record.mdb)+;DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};
set conn=server.CreateObject(adodb.connection)
conn.open connstr
dim n,rs,page,keyword,keywords,RecordCount,pageCount,pos,i,sql_where
n=50
page=リクエスト(ページ)
キーワード=トリム(リクエスト(キーワード))
sql_where=keyword_sousuo(タイトル,cz,名前,時間,キーワード)
set rs=server.CreateObject(adodb.recordset)
rs.open select * from records where &sql_where& ID で並べ替えます desc,conn,1,2
ページ = 0 の場合
ページ=1
終了する場合
レコード数 = 0
rs.Eof を実行しないで実行します
レコード数 = レコード数 +1
rs.次へ移動
ループ
RecordCount=0 でない場合は、
rs.MoveFirst
終了する場合
pageCount=レコード数/n
pageCount=int(ページ数)
if (RecordCount mod n)>0 then
ページ数=ページ数 +1
終了する場合
%>
<スタイルタイプ=テキスト/css>
<!--
.STYLE3 {カラー: #666666}
-->
</スタイル>
<テーブル幅=100% ボーダー=0 セルスペース=0 セルパディング=0>
<form id=form1 name=form1 method=post action=?><tr><td align=center><input name=keyword type=text id=keyword value=<%=keyword%> size=30 />
<入力タイプ=送信名=送信値=検索/>
<span class=STYLE3>(キーワードはスペースで区切ります) </span></td>
</tr></form>
<tr>
<td height=5></td>
</テーブル>
<table width=600 border=1 align=center cellpadding=4>
<tr align=center>
<td width=131>タイトル</td>
<td width=131>アクション</td>
<td width=131>ユーザー名</td>
<td width=132>時間</td>
</tr>
<%pos=0
do while pos<(page-1)*n
位置=位置+1
rs.moveNext
ループ
i=0
rs.eof および i<n ではない場合
i=i+1%>
<tr align=center>
<td><%=keyword_tag(rs(タイトル),キーワード)%></td>
<td><%=keyword_tag(rs(cz),keyword)%></td>
<td><%=keyword_tag(rs(名前),キーワード)%></td>
<td><%=keyword_tag(rs(時間),キーワード)%></td>
</tr>
<%rs.movenext
曲がります
rs.close
rs=何も設定しない
接続閉じる
conn=何も設定しない
'================================================ = =
'関数名:keyword_souuo
'機能: SQL クエリ条件を生成します。
'パラメータ: table_field ------ テーブルのフィールド名 (カンマ区切り)
' キーワード ------ 検索キーワード(スペース区切り)
'戻り値: SQLクエリ条件
'================================================ = =
関数キーワード_sousuo(byvalテーブルフィールド,byvalキーワード)
dim str01,str02,キーワード,テーブルフィールド,i,j
table_fields=split(trim(table_field),,)
キーワード=split(トリム(キーワード),,)
if table_field<> then
str01=(&table_fields(0)& like '%&keyword&%'
j=0 から ubound(table_fields) まで
str01=str01& または &table_fields(j)& のように '%&keyword&%'
次
str01=str01&)
それ以外
response.Write(<script>alert('パラメータ エラー (空にすることはできません)!')</script>)
応答.End()
終了する場合
'全角→半角スペース
キーワード=置換(キーワード, , )
' while InStr(キーワード, )>0
' キーワード=置換(キーワード, , )
'ウェン
キーワード=分割(キーワード, )
ubound(キーワード)>0の場合
for i=0 から ubound(キーワード)
str02=str02&(&table_fields(0)& like '%&keywords(i)&%'
j=1 から ubound(table_fields) まで
str02=str02& または &table_fields(j)& のように '%&keywords(i)&%'
次
str02=str02&)
次
str02=(&replace(str02,)(,)and()&)
キーワード_ソウスオ=( & str01 & または & str02 & )
それ以外
キーワード_ソウスオ=str01
終了する場合
終了関数
'================================================ = =
'関数名:keyword_tag
'機能: 文字列内のキーワードを赤色でマークします。
'パラメータ: str ------ 文字列
' キーワード ------- キーワードをマークします (スペースで区切ります)
'戻り値: 文字列(html形式)
'================================================ = =
関数キーワードタグ(byval str,byval キーワード)
dim キーワード、str01、str02、i
'全角→半角スペース
キーワード=置換(キーワード, , )
'while InStr(キーワード, )>0
' キーワード=置換(キーワード, , )
'ウェン
str01=replace(str,キーワード,<font color=#ff0000>&キーワード&</font>)
キーワード=分割(キーワード, )
ubound(キーワード)>0の場合
str02=str
for i=0 から ubound(キーワード)
str02=replace(str02,キーワード(i),<font color=#ff0000>&キーワード(i)&</font>)
次
キーワードタグ=str02
それ以外
キーワードタグ=str01
終了する場合
終了関数
薄暗い
%> <tr align=center>
<td Colspan=4> <%=RecordCount%><font color=#FF0000><%=page%></font>/<%=pageCount%> ページ
<%=n%> ページ/ページの移動先:
<SELECT name=select onchange=window.open(this.options[selectedIndex].value,'_self') >
<%y=1
do while y<pagecount+1%>
<OPTION value=?page=<%=y%>&keyword=<%=server.URLEncode(keyword)%> <%if cstr(page)=cstr(y) then response.Write( selected) end if%> >ページ <%=y%></OPTION>
<%y=y+1
ループ%>
</SELECT></td>
</tr>
</テーブル>