| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 791 人关注过本帖
标题:if语句嵌套了十几层,有没有更好的方法_已OK
只看楼主 加入收藏
yangliangy
Rank: 1
等 级:新手上路
帖 子:144
专家分:0
注 册:2006-6-25
收藏
 问题点数:0 回复次数:3 
if语句嵌套了十几层,有没有更好的方法_已OK
如图:使用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
%>_

[[it] 本帖最后由 yangliangy 于 2008-3-19 22:06 编辑 [/it]]

无标题.JPG (9.46 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 嵌套 语句 
2008-03-19 11:29
kira007
Rank: 2
等 级:论坛游民
帖 子:294
专家分:27
注 册:2007-6-28
收藏
得分:0 
参考下下面的做法吧,或者能用得着

set rspp=Server.CreateObject("ADODB.RecordSet")
sqln="SELECT * FROM product WHERE recommend <>3 AND"
If pid<>"" Then
sqln=sqln&" mark="&pid&" AND"
end if
'商品属性条件
If sortid=2 Then
sqln=sqln&" recommend=2 AND"
end if
If sortid=1 Then
sqln=sqln&" new=1 AND"
end if
'搜索类型
if key<>"" and  selectm=1 then
sqln=sqln&" name like '%"&key&"%'AND"
end if
if key<>"" and  selectm=2 then
sqln=sqln&" introduce like '%"&key&"%'AND"
end if
if key<>"" and  selectm=3 then
sqln=sqln&" code like '%"&key&"%'AND"
end if
'类别浏览
if categoryid<>"" then
sqln=sqln&" categoryid="&categoryid&" AND"
end if
if smallclassid<>"" then
sqln=sqln&" smallclassid="&smallclassid&" AND"
end if
if id<>""then
sqln=sqln&" id="&id&" AND"
end if
if bigclassid<>""then
sqln=sqln&" bigclassid="&bigclassid&" AND"
end if


if right(sqln,5)="WHERE" then
sqln=left(sqln,clng(len(sqln))-5) '解决所有参数都为空的情况
end if
if right(sqln,3)="AND" then
sqln=left(sqln,clng(len(sqln))-3) '
end if
sqln=sqln&" ORDER BY mark asc,name asc,adddate asc"
rspp.open  sqln,conn,1,1

生活的理想,就是为了理想的生活。
2008-03-19 13:37
gdk2006
Rank: 4
等 级:业余侠客
威 望:8
帖 子:928
专家分:270
注 册:2006-7-2
收藏
得分:0 
I服了YOU,怎么写这么多的东西啊!直接一个判断不就行了吗??
给你写一个:
if 关键字<>"" then
    sql=sql&"title like '%"&关键字(变量)&"%' "
end if
if 类别<>"" then
    sql=sql&"and 类别 = '"&类别(变量)&"'"
end if
……
后面依次写出来就行了,像你上面的写法就想要用集合一样,还要考虑多种问题,真是麻烦的!

程序员的悲哀如何找女朋友?
追女解决方案百度“让她着迷”!
2008-03-19 13:38
yangliangy
Rank: 1
等 级:新手上路
帖 子:144
专家分:0
注 册:2006-6-25
收藏
得分:0 
以前一开始我也是这样弄的,但出问题了,搞定现在一直写得这么复杂.今天又试测了,这样就行了,哎,简单的问题复杂化了

http://www./blog  我的小站
2008-03-19 22:03
快速回复:if语句嵌套了十几层,有没有更好的方法_已OK
数据加载中...
 
   



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

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