| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1451 人关注过本帖, 1 人收藏
标题:[原创]发个按条件搜索分页时简单的方法(抛砖引玉)
只看楼主 加入收藏
google
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:3419
专家分:23
注 册:2005-11-1
结帖率:0
收藏(1)
 问题点数:0 回复次数:3 
[原创]发个按条件搜索分页时简单的方法(抛砖引玉)

这几天为别人做了一个学生信息管理系统,本人水平有限,但在做的过程中也小有收获,拿出一点来大家共同欣赏.
做过ASP的朋友都知道,在按条件搜索并分页显示的时候,点击所分出的页往往要加很多很多参数,而且参数有的时候并不是一定的,所以操作起来非常困难,我在这里来说说我是怎么解决的.

我的这个前台表单所涉及的搜索选项有按照:姓名,性别,学号,班级,身份证号等进行一项或多项的复合搜索,在每个选项
前都加一个复选框来表示是否按照这个条件搜索.例 复选框(stuname) 学生姓名:文本(stu_name) 单选按钮(表示是精确搜索还是模糊搜索)这种样式来做的,大家看我的后台应该可以看出前台的编辑样式(现在上网的机子没有IIS,不能贴出图来,郁闷中,等有图的时候把图给大家补上^_^)

现在来看后台是怎么做的,我把用户所有的搜索条件按照判断来构造成一个SQL语句.

<%

if Request.Form("Submit")="搜索" then '如果用户进行了搜索或者重新进行了搜索
'构造搜索的SQL语句
if Request("stuname")="yes" then '检测姓名是否被选中
stu_name=check(Request("stu_name"))
if Request("stu_name")="" then
Response.Write("<script language=javascript>alert('错误!按名称搜索时名称不能为空!')</script>")
Response.Write("<script language=javascript>window.location='admin_charge.asp'</script>")
Response.End()
else
if Request.Form("name_like")="like" then
stuname_sql=" stu_name like '%"&stu_name&"%' and"
else
stuname_sql=" stu_name='"&stu_name&"' and"
end if
end if
else
stuname_sql=""
end if
if Request("stusex")="yes" then '检测性别是否被选中
stu_sex=check(Request("stu_sex"))
stusex_sql=" stu_sex='"&stu_sex&"' and"
else
stusex_sql=""
end if

if Request("stuaddtime")="yes" then '检测入学时间是否被选中
stu_addtime=check(Request("stu_addtime"))
if stu_addtime="请选择" then
Response.Write("<script language=javascript>alert('错误!按入学时间搜索时入学时间不能为空!')</script>")
Response.Write("<script language=javascript>window.location='admin_charge.asp'</script>")
Response.End()
end if
stuaddtime_sql=" stu_addtime='"&stu_addtime&"' and"
else
stuaddtime_sql=""
end if
if Request("stuclass")="yes" then '检测班级是否被选中
stu_class=check(Request("stu_class"))
if stu_class="请选择" then
Response.Write("<script language=javascript>alert('错误!按班级搜索时班级不能为空!')</script>")
Response.Write("<script language=javascript>window.location='admin_charge.asp'</script>")
Response.End()
end if
stuclass_sql=" stu_class='"&stu_class&"' and"
else
stuclass_sql=""
end if
if Request("stulearnid")="yes" then
stu_learnid=check(Request("stu_learnid"))
if stu_learnid="" then
Response.Write("<script language=javascript>alert('错误!按学号搜索时学号不能为空!')</script>")
Response.Write("<script language=javascript>window.location='admin_charge.asp'</script>")
Response.End()
else
if Request("learnid_like")="like" then
stulearnid_sql=" stu_learnid like '%"&stu_learnid&"%' and"
else
stulearnid_sql=" stu_learnid='"&stu_learnid&"' and"
end if
end if
else
stulearnid_sql=""
end if
if Request("stuidnum")="yes" then
stu_idnum=check(Request("stu_idnum"))
if stu_idnum="" then
Response.Write("<script language=javascript>alert('错误!按身份证搜索时身份证不能为空!')</script>")
Response.Write("<script language=javascript>window.location='admin_charge.asp'</script>")
Response.End()
else
if Request("idnum")="like" then
stuidnum_sql=" stu_idnum like '%"&stu_idnum&"%' and"
else
stuidnum_sql=" stu_idnum='"&stu_idnum&"' and"
end if
end if
else
stuidnum_sql=""
end if
if Request("stustart")="yes" then '检测学生是否初始化是否被选中
stu_start=check(Request("stu_start"))
stustart_sql=" stu_start='"&stu_start&"' and"
else
stustart_sql=""
end if
sql="select * from information where"&stuname_sql&stusex_sql&stuaddtime_sql&stuclass_sql&stulearnid_sql&stuidnum_sql&stustart_sql&" 1=1 order by stu_id desc"

'上面这句话是构造成的SQL语句,解释一下1=1的用途,如果用户什么都没有输入则直接按搜索按纽(本人较懒,没有写前台JAVASCRIPT验证,再说前台验证也只能对付菜鸟有用),如果不加这个1=1,则前面的where会引起SQL语句执行错误,加上在什么情况下都不会影响执行结果.

'下面来说说我是怎么传递参数的:同一条件的搜索结果,即使分了页,其SQL语句应该是一样的,这就要求我们要把相同的参数传递过去,以前做总是会在HREF里面加很多?来传递很多参数(特别是这种符合搜索,那样做更难),如果不想每次分页的时候连接上都有那么多字的话,那么我把这个SQL语句赋值给一个SESSION,SESSION是可以在不同页传递的,而在其它分页,我们只需要用CONN打开这个SESSION就可以了,代码如下:

session("sql")=sql
'Response.Write(sql)
end if
if session("sql")="" then
sql="select * from information order by stu_id desc"
session("sql")=sql
end if
%>



本程序版权为hxfly及编程中国论坛共同所有,如有转载,请注明出处

[此贴子已经被作者于2006-2-14 3:09:15编辑过]

搜索更多相关主题的帖子: 抛砖引玉 搜索 条件 
2006-02-14 02:31
google
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:3419
专家分:23
注 册:2005-11-1
收藏
得分:0 
分页的代码如下:
<%
set rs=Server.CreateObject("ADODB.RecordSet")
rs.open session("sql"),conn,3,1
if not rs.bof and not rs.eof then
rs.PageSize=20
PageNum=Request.QueryString("PageNum")
if PageNum="" then
rs.AbsolutePage=1
PageNum=1
else
rs.AbsolutePage=PageNum
end if
do while not rs.eof and i<rs.PageSize
%>
<tr>
<td class="tablecss2"><%=rs("stu_name")%></td>
<td class="tablecss2"><%=rs("stu_sex")%></td>
<td class="tablecss2"><%=rs("stu_learnid")%></td>
<td class="tablecss2"><%=rs("stu_class")%></td>
<td class="tablecss2"><%=rs("stu_age")%></td>
<td class="tablecss2"><%=rs("stu_idnum")%></td>
<td class="tablecss2" align="center"><%=rs("stu_start")%></td>
<td class="tablecss2"><a href="admin_inforshow.asp?stu_id=<%=rs("stu_id")%>" target="_blank">详情</a></td> <td class="tablecss2"><a href="admin_inforupdate.asp?stu_id=<%=rs("stu_id")%>">更新</a></td> <td class="tablecss2"><a href="admin_infordelete.asp?stu_id=<%=rs("stu_id")%>" onClick="return confirm('确定要删除这条记录吗?')">删除</a></td>
</tr>
<%
rs.movenext
i=i+1
loop
%>
</table>
<table width="780" border="0" align="center">
<tr>
<td width="461" align="right"><font color="#0000FF" size="2">共搜索到</font>&nbsp;<font color="#FF0000" size="2"><%=rs.RecordCount%></font>&nbsp;<font color="#0000FF" size="2">个符合条件的记录</font>
&nbsp;&nbsp;&nbsp;<font color="#0000FF" size="2">共</font>&nbsp;<font color="#FF0000" size="2"><%=rs.PageCount%></font>&nbsp;<font color="#0000FF" size="2">页</font>

<font color="#0000FF" size="2">当前为第</font>&nbsp;<font color="#FF0000" size="2"><%=PageNum%></font>&nbsp;<font color="#0000FF" size="2">页</font>
</td>
<td width="73" align="right">
<%
PPage=PageNum-1
If PPage>0 then
strPage="&nbsp;&nbsp;<font size=2 color=blue><a href=admin_charge.asp?PageNum="&PPage&">上一页</a></font>&nbsp;&nbsp;"
Response.Write(strPage)
end if
%>
</td>
<td width="160" align="center">
<%
For pn=1 to rs.PageCount
strURL="<font size=2 color=blue><a href=admin_charge.asp?PageNum="&pn&">"&pn&""&"</a></font>&nbsp;"
Response.Write(strURL)
Next
%>
</td>
<td width="68" align="left">
<%

NPage=PageNum+1
If NPage<=rs.PageCount then
strPage="&nbsp;&nbsp;<font size=2 color=blue><a href=admin_charge.asp?PageNum="&NPage&">下一页</a></font>"
Response.Write(strPage)
end if
%>
</td>
</tr>
</table>
<p align="right">&nbsp;</p>
<%
else
Response.Write("<script language=javascript>alert('对不起!没有搜索到符合条件的记录!')</script>")

end if
rs.close
set rs=nothing
conn.close
set conn=nothing
%>

[此贴子已经被作者于2006-2-14 2:45:14编辑过]


祝天下所有母亲幸福安康!~
2006-02-14 02:32
在编程中沦落
Rank: 2
等 级:新手上路
威 望:4
帖 子:411
专家分:0
注 册:2005-12-25
收藏
得分:0 
顶,我现在也正为这个搜索分页烦恼呢,得先认真研究一下楼主的,不过在看完之前先顶一下!

2006-02-16 12:20
yunfei111
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-10-10
收藏
得分:0 
顶楼主
2006-11-22 11:08
快速回复:[原创]发个按条件搜索分页时简单的方法(抛砖引玉)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016728 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved