...

View Full Version : Include Switch Question



MrTickles
04-28-2006, 07:16 AM
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

<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>

ralph l mayo
04-28-2006, 07:24 AM
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:


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';

}

MrTickles
04-28-2006, 02:45 PM
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

fci
04-28-2006, 02:56 PM
post the code and we can tell you what you did wrong.

MrTickles
04-28-2006, 03:06 PM
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


<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>

fci
04-28-2006, 03:12 PM
First, you have a syntax error and it is not actually including the file:

echo: 'body.php';

Second, why would the page=page1 load page3.php ? that is insanely non-intuitive, so, to simplify:


<?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');
}
?>

MrTickles
04-28-2006, 03:19 PM
Thank you very much for the help FCI.

Works perfectly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum