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