<title>在结果中再搜索</title>
<body bgcolor="#FFFFFF">
<%
u_search=request.form("u_search")
u_prev_search=request.form("u_prev_search")
u_search_within=request.form("u_search_within")
if u_search <> "" then
if u_prev_search = "" then
u_prev_search=u_search
else
u_prev_search=u_prev_search &","& u_search
g_prev_search=split(u_prev_search,",")
num_inputted=ubound(g_prev_search)
end if
sql= "select * from states where (capital like '%%"& u_search & "%%') "
if u_search_within = "Yes" then
for counter =0 to num_inputted-1
sql=sql& "and (capital like '%%"& g_prev_search(counter) & "%%') "
next
end if
accessdb="state_info"
cn="DRIVER={Microsoft Access Driver (*.mdb)};"
cn=cn & "DBQ=" & server.mappath(accessdb)
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, cn
' 如果没有找到相应的信息
if rs.eof then
%>
没有任何记录
<%' 有相应的信息就列出来
else
rs.movefirst
do while Not rs.eof
%>
<%= rs("capital") %><br>
<%
rs.movenext
loop
end if
end if
%>
<!-- Begin Form Input Area -->
<form action="<%= request.servervariables("script_name") %>" method="post">
<input type="text" name="u_search" value="<%= u_search %>">
<br>
<%
if u_search <> "" then %>
<input type = "radio" name="u_search_within" checked value="No"> 重新搜索
<input type = "radio" name="u_search_within" value="Yes"> 在结果中搜索
<%
if u_search_within = "Yes" then %>
<input type = "hidden" name="u_prev_search" value="<%= u_prev_search %>">
<%
else %>
<input type = "hidden" name="u_prev_search" value="<%= u_search %>">
<% end if%>
<br>
<% end if%>
<input type="submit" value="搜索">
</form>
<!-- End Form Input Area -->
<p> </p>
<%= sql %>
我是个菜鸟,解释得越详细越好。我可以根据情况适当加分。
參考答案:LZ给分, 加分啊
<body bgcolor="#FFFFFF">
<%
'为u_searc、u_prev_search、u_search_within赋值,值均为表单传递过来的数值
u_search=request.form("u_search")
u_prev_search=request.form("u_prev_search")
u_search_within=request.form("u_search_within")
'判断赋值语句A开始:当 u_search 不为空值的时候
if u_search <> "" then
'判断赋值语句B开始,注意该判断的前提是A,包含在A判断内
if u_prev_search = "" then '当u_prev_search为空,则把u_search 的值给 u_prev_search
u_prev_search=u_search
else
u_prev_search=u_prev_search &","& u_search '当u_prev_search也不为空,那么u_prev_search的值得等于u_prev_search与u_search的合并值,两值连接处以逗号,隔开,比如u_prev_search=2 u_search=1,那么现在u_prev_search的值为2,1 而不是3 ,也不是1,2
g_prev_search=split(u_prev_search,",") '两者均不为空时,u_prev_search获得合并值后,新建1个名为g_prev_search的数组,split函数的结果就是数组,比如split("cababae","a"),获得的数组为(c,b,b)
num_inputted=ubound(g_prev_search) '使用ubound函数计算该数组长度,可以确定其数组下标,用num_inputted保存该长度值,比如在split("cababae","a")中,数组(c,b,b)下标为2,有3个数值
end if 'B判断赋值结束
sql= "select * from states where (capital like '%%"& u_search & "%%') " '按照 u_search的值来找出数据库中,凡包含该值的数据
if u_search_within = "Yes" then 'C判断开始,
for counter =0 to num_inputted-1 '如果u_search_within的值为Yes 那么执行循环,循环次数是num_inputted,即有数组有即个数,就循环几次,num_inputted-1是因为FOR循环的特点,实际是循环了足够次数
sql=sql& "and (capital like '%%"& g_prev_search(counter) & "%%') " '循环的语句,分别按数组中的值查找出包含该值的数据
next 'FOR循环结束
end if 'C判断结束
accessdb="state_info" '数据库所在文件夹
cn="DRIVER={Microsoft Access Driver (*.mdb)};" '数据库驱动
cn=cn & "DBQ=" & server.mappath(accessdb) '获得数据库连接
Set rs = Server.CreateObject("ADODB.Recordset") '创建数据库实例
rs.Open sql, cn '打开数据库
'D判断开始
if rs.eof then ' 如果没有找到相应的信息
%>
没有任何记录
<%' 有相应的信息就列出来
else
rs.movefirst '找到相应记录,把指针移到这些记录的第1条
do while Not rs.eof '开始一个输出循环,输出显示每条记录的capital字段中的内容
%>
<%= rs("capital") %><br>
<%
rs.movenext '输出显示第1条后,指针下移1位,到第2条数据
loop '回到循环开始,执行循环
end if 'D判断结束
end if 'A判断结束
%>
<!-- Begin Form Input Area -->
<form action="<%= request.servervariables("script_name") %>" method="post">
<input type="text" name="u_search" value="<%= u_search %>">
<br>
<% 'E判断开始
if u_search <> "" then '如果u_search不为空,则显示下面的单选按钮%>
<input type = "radio" name="u_search_within" checked value="No"> 重新搜索
<input type = "radio" name="u_search_within" value="Yes"> 在结果中搜索
<% 'F判断开始
if u_search_within = "Yes" then '如果u_search_within 为YES,则设置隐藏变量u_prev_search,
%>
<input type = "hidden" name="u_prev_search" value="<%= u_prev_search %>">
<%
else '如果u_search_within 不为YES,则设置一个隐藏变量%>
<input type = "hidden" name="u_prev_search" value="<%= u_search %>">
<% end if 'F判断结束%>
<br>
<% end if 'E判断结束%>
<input type="submit" value="搜索">
</form>
<!-- End Form Input Area -->
<p> </p>
<%= sql '写出SQL值%>