if语句嵌套了十几层,有没有更好的方法

yangliangy1 2008-03-19 11:27:25


如图:使用if语句判断搜索,得判断十多次,具体我都有点说不清了,如下代码(有点多),用是有用,但觉得很繁锁,我一直是这么用的,如图,现在有4个条件,if语句写了那么多,如果条件更多的话那if语句则会更多,不知道不没有更好的判断方式.

<%
set rs = server.CreateObject("adodb.recordset")
keytext = replace(trim(Request("keytext")),"'","") '查询关键字
BigClassName = replace(trim(Request("BigClassName")),"'","") '类别
s_cl = replace(trim(Request("s_cl")),"'","") '范围
s_year = trim(Request("s_year")) '年
sql="select * from [product] where auditing=true "
if isnull(s_year) or s_year="" then
if isnull(s_cl) or s_cl="" then
if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and like '%"&keytext&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [name] like '%"&keytext&"%' order by sort",conn,1,1
end if
end if
else
if s_cl=1 then

if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and like '%"&keytext&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [name] like '%"&keytext&"%' order by sort",conn,1,1
end if
end if
elseif s_cl =2 then

if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [content] like '%"&keytext&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [content] like '%"&keytext&"%' order by sort",conn,1,1
end if
end if
end if
end if

else
if isnull(s_cl) or s_cl="" then
if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [name] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [name] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
end if
else
if s_cl=1 then

if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [name] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [name] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
end if
elseif s_cl =2 then

if isnull(keytext) or keytext="" then
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
else
if isnull(BigClassName) or BigClassName="" then
rs.open sql & " and [content] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
else
rs.open sql & " and BigClassName='"&BigClassName&"' and [content] like '%"&keytext&"%' and [date] like '%"&s_year&"%' order by sort",conn,1,1
end if
end if
end if
end if
%>
...全文
1707 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangliangy1 2008-03-20
  • 打赏
  • 举报
回复
明白了,以前一直没有拼接在一起,所以一直没想通,觉得应该将所有情况都判断,拼接一下,所有问题搞定,感谢各位高手相助~~~~
苍白之月 2008-03-19
  • 打赏
  • 举报
回复
1、sql拼接看作一个函数来处理 最后统一rs.open sql,conn,1,1 现有代码会简化一点 出错也会更容易调试
2、你的代码没问题 如果你的思路就是这样。。。。这句算我没说
3、下面的逻辑应该能满足你的需求 仔细研究


if 参数1 is not null then
sql=sql&"............"
elseif 参数2 is not null then
elseif 参数3 is not null then
elseif 参数4 is not null then
end if

yangliangy1 2008-03-19
  • 打赏
  • 举报
回复
对哦,翻页时可以将要传递的值全部写在一个参数中,然后用splice取出来
gingerkang 2008-03-19
  • 打赏
  • 举报
回复
sql语句可以拼接,参数一样可以拼接
不过我翻页传参数一般用get方式,在request.querystring中排除不要的就好了.
yangliangy1 2008-03-19
  • 打赏
  • 举报
回复
感觉代码太多,特别是分页的时候,要传递的参数太多了.
我现在想知道我这种程序思想是不是对的,比如现在四参数分别为1,2,3,4
我用if语句判断时分别要这样
if 1<>"" and 2<>"" and 3<>"" and 4<>"" then
...
elseif 1<>"" and 2<>"" and 3<>"" then
...
elseif 1<>"" and 2<>"" then
...
elseif 1<>"" and 3<>"" then
...
if 2<>"" and 3<>"" and 4<>"" then
...
if 2<>"" and 3<>"" then
...

再多就不写了,4个参数应该要写16个if
yangliangy1 2008-03-19
  • 打赏
  • 举报
回复
不能用select case,条件不一样,以前我问过,有的朋友说判断几次就行了,但我觉得行不通,那样很多条件会判断不到,下面是一位网友的回帖.


狂晕! 这么4个简单查询 你搞这个样子

我那客户查询提供15项查询 你不写累死了!

非常简单 4个查询 4个判断足够 某个判断多个值嵌套一层

keytext = replace(trim(Request("keytext")),"'","") '查询关键字
BigClassName = replace(trim(Request("BigClassName")),"'","") '类别
s_cl = replace(trim(Request("s_cl")),"'","") '范围
s_year = trim(Request("s_year"))

你的有一个固定条件 auditing=true

sql="select * from [product] where auditing=true "

然后每个一个加一个判断 就行了

if keytext<>"" then
sql=sql&" and [name] like '%"&keytext&"%' "
end if

if BigClassName<>"" then
sql=sql&" and BigClassName='"&BigClassName&"' "
end if

..... 把所有查询 都拼接起来

最后 sql=sql&" order by sort"

rs.open sql,conn,1,1

如果没有固定条件 就用 where 1=1 然后进行拼接sql查询语句。
snlixing 2008-03-19
  • 打赏
  • 举报
回复
用select case吧,还安全些
gingerkang 2008-03-19
  • 打赏
  • 举报
回复
眼睛都花了
yangliangy1 2008-03-19
  • 打赏
  • 举报
回复

上面那个路径看不到图片,这里补上
pgdoryoku 2008-03-19
  • 打赏
  • 举报
回复
用case吧.直观点
Atai-Lu 2008-03-19
  • 打赏
  • 举报
回复
select case xxx
case 1

case 2

case else

end select
tantaiyizu 2008-03-19
  • 打赏
  • 举报
回复
用过程
jnwentao 2008-03-19
  • 打赏
  • 举报
回复
设置表单或参数名与字段名相同,4个40个都只要这一段
dim f,sql:sql=""
'如果为get则request.form改为request.QueryString
for each f in request.form
if request.form(f)<>"" then
if not sql="" then sql =sql & " and "
sql = sql & f & "=" request.form(f)
end if
next
银狐被占用 2008-03-19
  • 打赏
  • 举报
回复
你自己都已经说出答案了。
就是楼上的方法。
Atai-Lu 2008-03-19
  • 打赏
  • 举报
回复

'//手误,郁闷...
'//我怎么觉着顶多需要四次if?
dim a1,a2,a3,a4
dim str1,str2,str3,str4
if a1<>"" then str1=" and aa='"&aa&"' "
if a2<>"" then str2=" and bb='"&bb&"' "
if a3<>"" then str3=" and cc='"&cc&"' "
if a4<>"" then str4=" and dd='"&dd&"' "
'//然后这里拼接
sql = sql&str1&str2&str3&str4

Atai-Lu 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yangliangy1 的回复:]
再多就不写了,4个参数应该要写16个if
[/Quote]


'//我怎么觉着顶多需要四次if?
dim a1,a2,a3,a4
dim str1,str2,str3,str4
if a1<> then str1=" and aa='"&aa&"' "
if a2<> then str2=" and bb='"&bb&"' "
if a3<> then str3=" and cc='"&cc&"' "
if a4<> then str4=" and dd='"&dd&"' "
'//然后这里拼接
sql = sql&str1&str2&str3&str4
pkkquan 2008-03-19
  • 打赏
  • 举报
回复
嗯...复杂...

用 7楼--- yangliangy1 那种方法就比较容易..而且比较方便
hj3793 2008-03-19
  • 打赏
  • 举报
回复
set rs = server.CreateObject("adodb.recordset")
keytext = replace(trim(Request("keytext")),"'","") '查询关键字
BigClassName = replace(trim(Request("BigClassName")),"'","") '类别
s_cl = replace(trim(Request("s_cl")),"'","") '范围
s_year = trim(Request("s_year")) '年

sql="select * from [product] where auditing=true "

Dim Search
Search = " where 1=1 "

if not isnull(BigClassName) or BigClassName!="" then Search = Search & "and BigClassName='"&BigClassName&"'"

if not isnull(keytext) or keytext!="" then Search = Search & "and [name] like '%"&BigClassName&"%'"

......

Search = Search & " order by sort"

rs.Open sql & Search,Conn,1,1
.........
可乐渴了吗 2008-03-19
  • 打赏
  • 举报
回复
同意 7楼的
看了看
学习了很多啊
yangliangy1 2008-03-19
  • 打赏
  • 举报
回复
whiteamoon,就这样4个条件判断就行吗?
假如现在1,4有值,则会执行elseif 参数4 后面的语句???
不晓得我的代码思路是否正确,

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧