SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Alternative to several if/then statements?

    I know this exists, I just don't know what it's called so I can search for it. (Newbie, can you tell?)

    I have several if/then statements that I want to combine. What is that called? Example...this is part of my code (and there's more!):

    Code:
    <%
    if page="links" then%>
    <!--#include file="links.htm"-->
    <%end if
    %>
    <%
    if page="homepg" then%>
    <!--#include file="homepg.htm"-->
    <%end if
    %>
    <%
    if page="id" then%>
    <!--#include file="id.htm"-->
    <%end if
    %>
    I know there's a way to tighten this up. Now, EVERY TIME I post here, I end up finding my answer within a few minutes...regardless of how much time I've already spent looking. We'll see what happens.

    But if you beat me to it, would you please tell me what that's called?

    THANKS!

  2. #2
    SitePoint Evangelist luxinterior's Avatar
    Join Date
    Aug 2004
    Location
    Here, there and everywhere!
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe your looking for 'case' statement...

    http://www.aspnetcenter.com/cliktopr...statements.asp

    Lux

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this ages ago, and it wasn't fun. I just gave up in the end, using pre-defined links.

    In the end I created a links.inc, then stuck this in:

    <%

    dim home
    home = "http://www.domain.com"
    dim products
    products = "http://www.domain.com/products/"

    %>
    then in the page you have added this to, just call a link like this <a href="<%= home %>">Home</a>

    this means you do not have to change your whole site when you change your links, also you can use a database, but pointless..

    Gav


  4. #4
    Guru Bullschmidt's Avatar
    Join Date
    Apr 2002
    Location
    USA
    Posts
    524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <%

    if page="links" then%>
    <!--#include file="links.htm"-->
    <%end if
    %>
    <%
    if page="homepg" then%>
    <!--#include file="homepg.htm"-->
    <%end if
    %>
    <%
    if page="id" then%>
    <!--#include file="id.htm"-->
    <%end if
    %>


    All the code in all the files would get included here (so for example you wouldn't be allowed to dim the same variables outside functions in 2 different included files) but only the code within the appropriate If Then would get acted upon.


    For dynamic includes you may want to look into Server.Execute like this:




    And in general don't forget about this possible structure for code:


    If blah1 Then

    ...

    Elseif blah2 Then

    ...

    Elseif blah3 Then

    ...

    Else

    ...

    End If
    J. Paul Schmidt
    www.Bullschmidt.com - Freelance Web and Database Developer
    www.Bullschmidt.com/DevTip.asp - Classic ASP Design Tips

  5. #5
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought I'd thanked you for yoru answer, and looking back I see I didn't...sorry. But THANK YOU! That was what I was looking for.

  6. #6
    Guru Bullschmidt's Avatar
    Join Date
    Apr 2002
    Location
    USA
    Posts
    524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome!
    J. Paul Schmidt
    www.Bullschmidt.com - Freelance Web and Database Developer
    www.Bullschmidt.com/DevTip.asp - Classic ASP Design Tips

  7. #7
    SitePoint Member oldwebgeek's Avatar
    Join Date
    Mar 2003
    Location
    New York
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want something like the following:

    Code:
    <%
    Select Case somevariable
        Case 1
            Do something
        Case 2
            Do something else
        Case 3
            Do something entirely different
        Case Else
            Do something if nothing matches any of the above
    End Select
    %>
    The "Case Else" is completely optional.

    You can use any variable, request.form or request.querystring item in place of "somevariable".

    Also, where you see "Case 1, 2, 3", the 1, 2 and 3 can be any string or number format such as "hello" or 10.
    Nolan Interactive: Web services and software for business
    Web Descant: Website Design and Marketing Blog
    No-Nonsense Hosting: Business Web hosting
    WebmailASP: Webmail client for ASP and IIS


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •