Need help programming IF else statement?

Discussion in 'PHP' started by Neo_The_One, Jun 21, 2009.

  1. #1
    Hi, Guys!

    I need some help programming some small piece of PHP code.

    I have a file index.php

    In there i want to show 1 type of content if users go directly to index.php.
    If they go to: index.php?id=index2

    Than i would like to replace the current content and show the content for "index2" f.eks

    <if index2>
    content here
    </close if>

    <if index3>
    content here
    </close if>


    Could anyone help me with this??

    Thanks!
     
    Neo_The_One, Jun 21, 2009 IP
  2. parkerproject

    parkerproject Active Member

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    98
    #2
    hey dude try this

    
    
    // check for _GET['page'] -> index.php?page= ...
    if (isset($_GET['page']))
    {
        $page = trim($_GET['page']); // delete whitespace
        
        // check what is requested
        switch($page)
        {
            case 'home': // if index.php?page=home then
                include('home.php');
            break; // jump out of switch()
            
            case 'about': // if index.php?page=about then ...
                include('about.php');
            break;
            
            default: // if something like index.php?page=blabla then
                include('home.php');
            break;
        }
    }
    
    PHP:
     
    parkerproject, Jun 21, 2009 IP
  3. Gonzo4u

    Gonzo4u Well-Known Member

    Messages:
    410
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Hello Neo_The_One,

    Instead of using multiple IF statements use Switch statement.

    Example:

    <?php

    $id = $_GET['id'];

    // Switch ID statement
    switch ($id) {
    case "index2":
    echo "content here";
    break; // jump out of switch()
    case "index3":
    echo "content here";
    break; // jump out of switch()
    default: // default action if no argument is passed or no match.
    echo "content here";
    break;
    }
    ?>


    Now you can also use 2 types of approach:
    1) Directly paste the content in case statement echo area or
    2) You can also call the external page by using include statement eg: include('home.php');

    Hope it helps you

    Regards,
    Gonzo
     
    Gonzo4u, Jun 21, 2009 IP
  4. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Hi,

    ^ this info look okey...

    But i need to replace the main content when the ID is present.

    + Anyway write the content within the same file, but not to use echo, because i need to use HTML / PHP code within it?


    So when i use the ID string it replaces the main content with the Content box i request.

    If no ID string is requested = show main content.
     
    Neo_The_One, Jun 21, 2009 IP
  5. Gonzo4u

    Gonzo4u Well-Known Member

    Messages:
    410
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    In that case the method will be the same, just use "include" to call your content html file.

    eg: include ("content1.html");
    and for main "main.html in default section of switch statement.

    Gonzo
     
    Gonzo4u, Jun 21, 2009 IP
  6. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #6
    I would have to agree with Gonzo4u
     
    dweebsonduty, Jun 27, 2009 IP