Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2004
    Location
    Mobile, AL
    Posts
    138

    PHP switch question

    Ok, so I'm new to switch constructs, I've always used if/else in previous creations. For some reason I can't get 1 of 3 things to show up. I get no errors, but just nothing shows up.

    The scenario is, I have a file, mk.php, and in it is my switch code. If nothing exists after mk.php then show one thing, then if there are variables after mk.php in the URL show something else and so on.

    Here's the code I have so far:

    PHP Code:
    <? 

    if ( !$_GET['land'] ) {
       include( 
    'parks/mk/mkintro.php' ); 
    } else {

       switch( 
    $_GET['land'] ) { 

             case 
    adventure:   

                switch( 
    $_GET['attraction'] ) { 
                case 
    junglecruise
                include( 
    'parks/mk/adventureland/junglecruise.php' ); 
                break; 
                }

             break; 

       default:
       include( 
    'parks/mk/adventureland/adventureland.php' ); 
       break;

       }

    }

    ?>
    If you go to mk.php, mkintro.php shows like it's supposed to.

    If you go mk.php?land=adventure&attraction=junglecruise, junglecruise.php shows as it's supposed to.

    But if you go to mk.php?land=adventure, nothing shows although I want adventureland.php to show up. There will be more attractions listed, I'm just testing with one right now.

    Any ideas on what I'm doing wrong? Feel free to correct me, as I said, this is my first time attemtping to use switch.

    edit: Now that I look at it, I know it won't work like I want it, because there will be other lands besides Adventureland, so fro what I have adventureland.php is always going to be default no matter what the land is. So I guess I need alot more fixed than what I thought.
    Shawn Kerr

  2. #2
    Join Date
    Jan 2004
    Location
    Mobile, AL
    Posts
    138
    Woohoo! I think I figured it out. At least it works for now, not sure how it will react when I add the rest of the lands and attractions.

    I guess a little sleep after staring at the monitor for more than 24 hours helps.
    Shawn Kerr

  3. #3
    Join Date
    Dec 2002
    Location
    The Shadows
    Posts
    2,925
    okay, here is your problem. When you use the switch clause, you need a default case, otherwise it won't show anything, or do anytihng, if the variable is not set.

    Now, you are missing a default case for the "attractions" switch. Add a default directive for that, and all will be well, hopefully
    Dan Sheppard ~ Freelance whatever

  4. #4
    Join Date
    Jan 2004
    Location
    Mobile, AL
    Posts
    138
    Well, I got it working without a default case, just throwing another if/else inside the case. But I guess that's doing pretty much the same thing, if there is no variable, then include this as "default". It works for what I need anyway.

    I've already added other lands and attractions and it's all working.

    PHP Code:
    <? 

    if ( !$_GET['land'] ) {
       include( 
    'parks/mk/mkintro.php' ); 
    } else {

       switch( 
    $_GET['land'] ) { 

             case 
    adventure:   

                if ( !
    $_GET['attraction'] ) {
                include( 
    'parks/mk/adventureland/adventureland.php' ); 
                } else {

                   switch( 
    $_GET['attraction'] ) { 
                   case 
    junglecruise
                   include( 
    'parks/mk/adventureland/junglecruise.php' ); 
                   break; 
                   }

                }

             break; 

       }

    }

    ?>
    Shawn Kerr

  5. #5
    Join Date
    Feb 2003
    Posts
    717
    you should put quotes in the case statement:
    PHP Code:
    <? 
    switch( $_GET['attraction'] ) { 
    case 
    "junglecruise"
    include( 
    'parks/mk/adventureland/junglecruise.php' ); 
    break; 


    ?>
    I don't know if its required (well I guess not if its working for you) but I prefer to enclose words with quotes, to avoid any potential problems, you should also use the default thing instead of an if/else, otherwise if kind of defeats the purpost for a switch statement (and its prob a wee bit faster).


    - my .02 cents

  6. #6
    Join Date
    Jan 2004
    Location
    Mobile, AL
    Posts
    138
    Ok. I switched to the default bit, and it's still working! I think I was just sticking it in the wrong place when I tried before. And yeah, I'll add the quotes. I'm usually a quote freak with everything, but the site that had the tutorial didn't have them, and since it was my first shot with switch, I didn't know if it needed them, or was better to have them or anything.

    And thanks for the help/advice, both of you.
    Shawn Kerr

  7. #7
    Join Date
    Dec 2002
    Location
    The Shadows
    Posts
    2,925
    It should look similar to this:

    PHP Code:
    <? 
    switch( $_GET['land'] )
    {
         case 
    'adventure':
              switch( 
    $_GET['attraction'] )
              {
                   case 
    'junglecruise':
                        include( 
    'parks/mk/adventureland/junglecruise.php' );
                        break;
                   default:
                        include(
    'parks/mk/adventureland/adventureland.php');
              }
              
              break;
         default:
              include( 
    'parks/mk/mkintro.php' );
              break; 


    ?>
    Dan Sheppard ~ Freelance whatever

  8. #8
    Join Date
    Jan 2004
    Location
    Mobile, AL
    Posts
    138
    Ok, I'm all straight now, no more if/else's. I guess I'm stil trying to rely on them to accomplish things because I know them well.

    One last question, does the default have to be at the bottom or is it just preferred? I mean I've got it working at the top, but the examples I've seen and the examples here have it at the bottom.
    Shawn Kerr

  9. #9
    Join Date
    Dec 2002
    Location
    The Shadows
    Posts
    2,925
    No, it doesn't have too. It should work anywhere you want. I just put it at the bottom. I am sure there are people here that use it at the top.

    If it is at the top, make sure you don't forget the break; like I did above.
    Dan Sheppard ~ Freelance whatever

  10. #10
    Join Date
    Jan 2003
    Posts
    1,715
    That would be interesting to test. I always assumed it took the first match, which would mean default would eat everything.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

Posting Permissions

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