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 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Include Switch Question

    Hello,

    I am trying to use the code to change pages from a menu link. It does accomplish that except link 1 will include page2 and page3 information. Page2 includes page3 information. Finally page3 only contains page3 information.

    I need each link to only display the called page information. What I am doing is I have a 3 column built. Index.php has includes for the header.php, left_menu.php, right_menu.php, footer.php, and the body.php

    I am creating a menu to different pages within the website and my goal is to have each link only change the content of body.php. So if I click on a link to page1, page1 content will load in the body area. Click on page2 and page2 content will load into the body area w/o the page1 content being there.

    hope this makes sense. If you know a better way to accomplish this with out the use of frames that could work as well. Thanks
    Code:
    <html>
    <head>
    <title>My Page</title>
    </head>
    <body>
    
    <!-- Page Header -->
    <?php
    include("header.php");
    ?>
    <!-- End Page Header -->
    
    <a href="index.php?page=page1">Click for page 1</a><br />
    <a href="index.php?page=page2">Click for page 2</a><br />
    <a href="index.php?page=page3">Click for page 3</a><br /><br />
    
    <!-- Page Content -->
    <?php
    switch($_GET['page']){
    case "page1":
    include("page3.php");
    case "page2":
    include("page2.php");
    case "page3":
    include("page1.php");
    }
    ?>
    <!-- End Page Content -->
    
    </body>
    </html>

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    The default behavior of switch statements is to "fall through" to every consecutive case once any condition matches. You have to forcibly exit the structure with break to prevent this behavior. Eg:
    PHP Code:
    switch ((int) $num)
    {
        case 
    1// fall through
        
    case 2// fall through
        
    case 3:
            echo 
    '1, 2, or 3';
        break; 
    // end fall by forcing exit
        
    case 4:
        case 
    5:
            echo 
    '4 or 5';
        break;
        default:
            echo 
    '<1 || >5';


    Last edited by ralph l mayo; 04-28-2006 at 06:33 AM.

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply.

    Unfortunatley I don't understand what your talking about. I tried to add breaks into the code and they didn't stop the pages from loading on top of one another.

    Perhaps someone could explain this to me a little better.

    Thank you

  • #4
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    post the code and we can tell you what you did wrong.

  • #5
    New Coder
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heres the edit I made to the above code. This is going to be molded into a menu eventually. So the links that you click to change the content in the middle section of the page will be residing on a menu located in the header.php. Will this add later problems as well? Thank you

    Code:
    <html>
    <head>
    <title>My Page</title>
    </head>
    <body>
    
    <!-- Page Header -->
    <?php
    include("header.php");
    ?>
    <!-- End Page Header -->
    
    <a href="index.php?page=page1">Click for page 1</a><br />
    <a href="index.php?page=page2">Click for page 2</a><br />
    <a href="index.php?page=page3">Click for page 3</a><br /><br />
    
    <!-- Page Content -->
    <?php
    switch($_GET['page']){
    case "page1":
    include("page3.php");
    break;
    case "page2":
    include("page2.php");
    break;
    case "page3":
    include("page1.php");
    break;
    default:
    	echo: 'body.php';
    }
    ?>
    <!-- End Page Content -->
    
    </body>
    </html>

  • #6
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First, you have a syntax error and it is not actually including the file:
    Code:
    echo: 'body.php';
    Second, why would the page=page1 load page3.php ? that is insanely non-intuitive, so, to simplify:
    Code:
    <?php
        switch($_GET['page']){
            case 'page1':
            case 'page2':
            case 'page3':
                // this means it will load any of the above case statements with a .php at the end
                include($_GET['page'].'.php');
                break;
            default:
                include('body.php');
        }
    ?>

  • #7
    New Coder
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much for the help FCI.

    Works perfectly.


  •  

    Posting Permissions

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