PHP where you are links help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

PHP where you are links help

Post by tomsace »

Hey, I use the php command on my website to get multiple pages out of one php file.
The code I am using is similar to this:

Code: Select all

 
<?php
 
$cmd = $_GET['cmd'];
if ($cmd=="") { $cmd = "page1.php"; } 
 
@include('header.php');
 
switch($cmd)
{
 
case "page1":
@include('page1.php');
break;
 
case "page2":
@include('page2.php');
break;
 
case "page3":
@include('page3.php');
break;
 
}
@include('footer.php');
 
?> 
 
This is so I can just type the content of each page in page1.php page2.php and page3.php. But what I need help with is also adding in the header file where the user is on each page.

For example: Home > Products > Shoes > Nike etc...

How can I edit my script so that I can also change the menu on each page?

Thanks in advance.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: PHP where you are links help

Post by watson516 »

Providing the menu doesn't come before the switch statement you can use a session to keep track of the current page.

Code: Select all

<?php
session_start();
//Put whatever code you want here
switch($cmd)
{
      case "page1":
             $_SESSION['page']="page1";
             include('page1.php');
             break;
      case "page2":
             $_SESSION['page']="page2";
             //Rest of code
}
 
If the menu comes before this switch statement, ie in the header.php file, this won't work because the session variable gets set after the menu is rendered. In that case, you could put a switch into your menu code to check the value of $_GET['cmd'] and change it right inside the menu.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP where you are links help

Post by Burrito »

please use PHP tags next time you post code in the forums.

in your header.php file:

Code: Select all

 
$breadcrumbs->show();
 
in your switch() (would need to be done above your call to header.php)

Code: Select all

 
$breadcrumbs->set($_GET['cmd']);
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP where you are links help

Post by tomsace »

Thanks alot everyone. Got this working perfectly.
Post Reply