shopsearch.asp

来自「ASP+SQL Server动态网站开发从基础到实践教程」· ASP 代码 · 共 161 行

ASP
161
字号
<%Option Explicit%>
<!--#include file="shop$db.asp"-->
<%

Session("CurrentURL")="shopsearch.asp"
Saction=Request.Form("Action")

Dim Words(10)
Dim wordcount
Dim delimiter
Dim sAction
Dim strKeyword
Dim rscat
Dim dbc
dim Rssubcat
Dim sqlSub
If SAction="" then
     ShopOpenDatabase dbc
     ShopPageHeader
     SQL = "SELECT * from categories"
     Set rscat = dbc.Execute(SQL)
     SearchDisplayForm()
     ShopCloseDatabase dbc
     ShopPageTrailer
Else
    Response.write "getting form data"
    SearchGetFormData
    SearchGenerateSQL      '产生查询数据函数
    Response.Redirect "ShopDisplayproducts.asp?Search=Yes"
End if

' 该函数产生数据库
Sub SearchGenerateSQL()
dim i
dim whereok
whereok=" WHERE "
SQL = "SELECT * FROM Products " 
if wordcount> 0  then
	SQL = SQL & whereok 
	SQL = SQL & "("
	Whereok="" 
	for i = 0 to wordcount-1
		SQL=SQL & whereok
		SQL = SQL & " ( CDescription Like '%" & words(i) & "%' "
		SQL = SQL & " OR    CName     like '%" & words(i) & "%'"
		SQL = SQL & " OR    MFG       like '%" & words(i) & "%'"
		SQL = SQL & " )"
		whereok=" OR "
	next
	SQL = SQL & ")"
	whereok=" AND "
end if    
if strCategory <> "" then
   SQL=SQL & whereok 
   SQL = SQL &  " CCategory = " & strCategory 
   whereok=" AND "
end if
if strSubCategory <> "" then
   SQL=SQL & whereok 
   SQL = SQL &  " Subcategoryid = " & strSubCategory 
end if
Session("SQL")=SQL
Session("words")=Words
Session("wordcount")=wordcount
end sub

Sub SearchDisplayForm()
	Dim sRowColor
	sRowColor="#C4CEE5"
        Response.Write("<blockquote>")
%>
<p align="center"><font size="2" color="#660000"><b><i>搜索全部商品或者限定某一类别商品<br>
通过关键子搜索,关键字不要超过10个字符,可以通过逗号隔开</i></b></font></p>
<%
	Response.Write("<h2>输入搜索参数</h2>")
if sError <>"" then
	Response.Write("<font color=red>" & sError & "</font><p>")
end if
	Response.Write("<form name=form1 method=Post action=shopsearch.asp>")
	Response.Write("<table cellpadding=2 cellspacing=2>")
	Response.Write("<tr bgcolor=" & sRowColor &"><td>关键字:</td><td><input size=50 name=Keyword></td></tr>")
	Response.Write("<tr bgcolor=" & sRowColor &"><td>商品目录:</td><td>")
%>

  <select size="1" name="Category">
    <option>All</option>
<%
Do While NOT RSCat.EOF
   If RSCat("Catdescription") <> "" Then
     response.write "<option value=" & RsCAt("CategoryID") & ">" & RSCat("Catdescription") & "</option>"
   End If
   RSCat.MoveNext
   Loop 
   rscat.close
%>
  </select>&nbsp;</b></font></p>
</td></tr>
<%
	Response.Write("</table><p>")
	Response.Write("<input type=submit name=action value=搜索>")
	Response.Write("</form>")
	Response.Write("</blockquote>")
 End Sub
Sub SearchGetFormData()
strCategory = Request.Form("Category")
if strCategory="All" then
   strCategory=""
end if
strSubCategory = Request.Form("SubCategory")
if strSubCategory="All" then
   strSubCategory=""
end if
strKeyword = Request.Form("Keyword")
if strkeyword<>"" then
  Delimiter=","
  parseRecord strkeyword, words, wordcount,delimiter
Else
  wordcount=0
end if
end sub
Sub ParseRecord (record,words,wordcount,delimiter)
Dim pos 
Dim recordl
Dim bytex
Dim temprec
Dim maxwords
Dim i
maxwords = 10
temprec = record
Dim maxentries
pos = 1
wordcount = 0
' 确保数组是空的
maxentries = UBound(words)
For i = 0 To maxentries - 1
   words(i) = ""
Next
recordl = Len(temprec)
' 首先取消开始的空格
Do
   bytex = Mid(temprec, pos, 1)
   While bytex = " " And pos <= recordl
     pos = pos + 1
     bytex = Mid(temprec, pos, 1)
   
   Wend
' 将关键字加入到数组中
   While bytex <> delimiter And pos <= recordl
     words(wordcount) = words(wordcount) & bytex
     pos = pos + 1
     bytex = Mid(temprec, pos, 1)
     
   Wend
   wordcount = wordcount + 1
   pos = pos + 1
   If wordcount > maxentries Then Exit Sub
Loop Until pos > recordl

End Sub

%>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?