Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Get variable for URL

    PHP Code:
    <? if($_GET['m'] == "page1" || !isset($_GET['m'])){ ?>
    a
    <? }
    else if(
    $_GET['m'] == "page2"){ ?>
    b
    <? ?>
    It's not working :[

    I was trying to have different "pages" in a single php file, but it doesn't seem to be working. If I'm doing this the wrong way, let me know!
    Last edited by RonnyNishimoto; 08-03-2012 at 11:53 AM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by RonnyNishimoto View Post
    PHP Code:
    <? if($_GET['m'] == "page1" || !isset($_GET['m'])){ ?>
    a
    <? }
    else if(
    $_GET['m'] == "page2"){ ?>
    b
    <? ?>
    It's not working :[

    I was trying to have different "pages" in a single php file, but it doesn't seem to be working. If I'm doing this the wrong way, let me know!
    first <? is frowned upon please use <?PHP instead,
    you need to wrap each if in brackets, then the whole if in brackets:

    PHP Code:
    <?PHP
    if (($_GET['m'] == "page1") || (!isset($_GET['m']))) {
        echo 
    "a";
    }
    else if (
    $_GET['m'] == "page2"){ 
        echo 
    "b";

    ?>

  • Users who have thanked Arcticwarrio for this post:

    RonnyNishimoto (08-03-2012)

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    also if you have a few pages you want to use, consider using switch instead:

    PHP Code:
    <?PHP
    switch ($_GET['m']){
        
        case 
    'page1':
        echo 
    "a";
        break;
        
        case 
    'page2':
        echo 
    "b";
        break;
        
        case 
    'page3':
        echo 
    "c";
        break;
        
        default:
        
    //w what to do if $_GET['m'] isnt set
        
    }
    ?>

  • Users who have thanked Arcticwarrio for this post:

    RonnyNishimoto (08-03-2012)

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    It worked!

    I copy code from tutorials and often times they are wrong lol...

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by RonnyNishimoto View Post
    I copy code from tutorials and often times they are wrong
    the human error, also called low quality. if I’d find a tutorial to be either out of date or containing errors, I’d discard it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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