Page 1 of 4 1234 LastLast
Results 1 to 10 of 33
  1. #1
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default PHP Switch - mini tutorial.

    Hey, just wanted t share a cool little function I once came upon a while back when I was new to PHP. The name itself is pretty self explanatory allowing for you to easily switch through pages, so lets start.

    Start off with a base code like this:
    PHP Code:
    <?php

    switch($_GET[page]) {
    default:
    echo 
    "<a href='?page=hello'>Hello</a>";
    break;

    case 
    'hello':
    echo 
    "Hello";
    break;
    }

    ?>
    Now let me explain what this does, first of all we initiate the switch function then use the $_GET statement to grab the page we want to view.

    default: is basically the default page upon page load, so if you went to www.site.com/index.php you'd get a link with "Hello" on it. We then use the break; statement to break the page.

    Case.
    We use the case statement to grab the end of the url from the default echo which in our case was hello, this allows us to link to an entirely different page using that simple feature.

    Have fun!
    Oh, you can also do multiple clauses by using:

    PHP Code:
    <?php

    switch($_GET[page]) {
    default:
    echo 
    "<a href='?page=hi'>Hi</a>";
    break;

    case 
    'hi':
    echo 
    "<a href='?page=bonjour'>Bonjour</a>";
    break;

    case 
    'bonjour':
    echo 
    "Bonjour!";
    break;
    }

    ?>
    See what I did?

    Closed by Invent (Forum Moderator): Thread closed due to prevent (further) arguments.
    Last edited by Invent; 12-09-2008 at 09:52 PM.
    Back for a while.

  2. #2

    Default

    You need to add slashes before your quotes so it doesn't think you're ending the string.

    PHP Code:
     <?php

    switch($_GET[page]) {
    default:
    echo 
    "<a href\"=?page=hi\">Hi</a>";
    break;

    case 
    'hi':
    echo 
    "<a href=\"?page=bonjour\">Bonjour</a>";
    break;

    case 
    'bonjour':
    echo 
    "Bonjour!";
    break;
    }

    ?>
    Also, you missed a quote in your first <a> tag

    Nevermind. I see you've edited it now anyway
    Last edited by Jme; 11-09-2008 at 10:18 PM.

  3. #3
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Delete, forum lagg.
    Last edited by Protege; 11-09-2008 at 10:25 PM.
    Hi, names James. I am a web developer.

  4. #4
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    I think php.net would help them better tbh. Seeing as you can't even get the alignment right in a switch, making it confusing for even me so even more confusing for a beginner.

    I don't think you needed to post this. Sorry.

    $_GET[ 'page' ] or $_GET[ "page" ]

    & default is suppose to be at the bottom.

    @ Invent, support untidy coding +rep
    Last edited by Protege; 11-09-2008 at 10:24 PM.
    Hi, names James. I am a web developer.

  5. #5
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Seeing as you can't even get the alignment right in a switch
    What do you mean by that? Indentation? If so, indenting code is a personal preference and you don't have to do it.

    So you think you should have to indent code which is a personal preference? Then why the hell do you and other people have a go at charlie when he says things about using parenthesis with language constructs when you're doing the exact same thing moaning about indentation?
    Last edited by Invent; 11-09-2008 at 10:29 PM.

  6. #6
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Jme View Post
    You need to add slashes before your quotes so it doesn't think you're ending the string.

    PHP Code:
     <?php

    switch($_GET[page]) {
    default:
    echo 
    "<a href\"=?page=hi\">Hi</a>";
    break;

    case 
    'hi':
    echo 
    "<a href=\"?page=bonjour\">Bonjour</a>";
    break;

    case 
    'bonjour':
    echo 
    "Bonjour!";
    break;
    }

    ?>
    Also, you missed a quote in your first <a> tag

    Nevermind. I see you've edited it now anyway
    Yeah the proper way would be for slashes but for mini things like this tutorial for beginners I just use '' Yeah I edited haha, saw the rest of the code was red.

    Quote Originally Posted by Protege View Post
    I think php.net would help them better tbh. Seeing as you can't even get the alignment right in a switch, making it confusing for even me so even more confusing for a beginner.

    I don't think you needed to post this. Sorry.
    The alignment is fine and indentation is not something I always use..

    Quote Originally Posted by Invent View Post
    What do you mean by that? Indentation? If so, indenting code is a personal preference and you don't have to do it.
    Yeah he's on about indentation which as you said is a personal preference, he's just being picky to my work.
    Back for a while.

  7. #7
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    It should be indented though for a tutorial, as you're teaching people who do not know how to code.. incorrect procedures..

    They won't learn to indent their code.

    Plus default: should be at the bottom.

  8. #8
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    +rep Excellent2

    Nice work

    +rep to Invent as I need to spread.
    Hi, names James. I am a web developer.

  9. #9
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3,670
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Dentafrice View Post
    It should be indented though for a tutorial, as you're teaching people who do not know how to code.. incorrect procedures..

    They won't learn to indent their code.

    Plus default: should be at the bottom.
    I'll remember that for my next tutorial
    Back for a while.

  10. #10
    Join Date
    Sep 2006
    Location
    Evanston, Illinois.
    Posts
    2,361
    Tokens
    0

    Latest Awards:

    Default

    switch isn't a function.

    :rolleyes:

    If your going to make a tutorial, at least code it well, indentation, having default at the bottom, using strings in your get array.

    oh denta just said the exact same thing... thats like a teacher teaching you to code in tables -- you shouldn't do it.
    How could this hapen to meeeeeeeeeeeeeee?lol.

Page 1 of 4 1234 LastLast

Posting Permissions

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