Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2006, 06:16 AM   PM User | #1
MrTickles
New Coder

 
Join Date: Sep 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
MrTickles is an unknown quantity at this point
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>
MrTickles is offline   Reply With Quote
Old 04-28-2006, 06:24 AM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
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..
ralph l mayo is offline   Reply With Quote
Old 04-28-2006, 01:45 PM   PM User | #3
MrTickles
New Coder

 
Join Date: Sep 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
MrTickles is an unknown quantity at this point
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
MrTickles is offline   Reply With Quote
Old 04-28-2006, 01:56 PM   PM User | #4
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
post the code and we can tell you what you did wrong.
fci is offline   Reply With Quote
Old 04-28-2006, 02:06 PM   PM User | #5
MrTickles
New Coder

 
Join Date: Sep 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
MrTickles is an unknown quantity at this point
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>
MrTickles is offline   Reply With Quote
Old 04-28-2006, 02:12 PM   PM User | #6
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
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');
    }
?>
fci is offline   Reply With Quote
Old 04-28-2006, 02:19 PM   PM User | #7
MrTickles
New Coder

 
Join Date: Sep 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
MrTickles is an unknown quantity at this point
Thank you very much for the help FCI.

Works perfectly.
MrTickles is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:20 PM.


Advertisement
Log in to turn off these ads.