Page 1 of 1

Dynamic Text Navigation Menu and included pages - how?

Posted: Thu Jun 30, 2005 11:31 am
by CutAndPaste
Hi,

I want to create a text based menu system for some included pages on a main index.php. The menu should reflect the page displayed and its position in the menu structure

The current index.php page has essentially got three areas in columns, Left Nav, Central Content and Right Content. Left Nav and Right Content are the same on every page, Central Content will alter depending upon the menu cxhoice made.

I want to create a system whereby when the user clicks on a link in Left Nav the content in the middle section updates to include the desired page info and the external LeftNav & Right Content remains the same.

I also want to have the menu system reflect the page being displayed so that
MenuItem1
MenuItem2
MenuItem3
MenuItem4
So when I click on MenuItem1 I get
MenuItem1
>>SubMenuItem1-Option1
>>SubMenuItem1-Option2
MenuItem2
MenuItem3
MenuItem4
So when I click on SubMenuItem1-Option1 I get
MenuItem1
>>SubMenuItem1-Option1
>>>>SubMenuItem1-Option1Choice1
>>>>SubMenuItem1-Option1Choice2
MenuItem2
MenuItem3
MenuItem4
So when I click on SubMenuItem1-Option1Choice1 I get the page I want included in the central area of my main standard page.

Someone told me they'd done some asp code that did this, but I'm not sure how to transfer this into php.

I think it essentially involved a process that went along the lines of:

If the "MenuItem1" is clicked, include beneath it:
>>SubMenuItem1-Option1 and
>>SubMenuItem1-Option2.

Then, if SubMenuItem1-Option1 was clicked, include beneath it >>>>SubMenuItem1-Option1Choice1 and
>>>>SubMenuItem1-Option1Choice2

Then if SubMenuItem1-Option1Choice1 was clicked, load the Option1Choice1.php page into the required palce on the index.php page.


I'd probably also want to set the broswer page title to reflect the included page as well.

Is this a crazy thing for a relative noob to take on?

Any help/code snips/advice/tutorials you can point me to?

thanks.

Posted: Thu Jun 30, 2005 11:52 am
by Burrito
you have several options for doing this and it's been discussed a lot on this forum. You might try searching the forum for "nav" or "navigation" etc if you don't like the answer I provide.

the way I'm going to outline will involve using one page "index.php" and include your middle pane by including other php pages, but it is by no means the only or "best" way to do it.

Code: Select all

//left nav section
echo &quote;<a href=\&quote;index.php?cpane=product\&quote;>Products</a>&quote;;
if(isset($_GETї'pane']) && $_GETї'pane'] == 'product'){
   echo &quote;  <a href=\&quote;index.php?cpane=product&prod=widgets\&quote;>Widgets</a>&quote;;
   echo &quote;  <a href=\&quote;index.php?cpane=product&prod=hammer\&quote;>Hammers</a>&quote;;
} // end if for products
echo &quote;<a href=\&quote;index.php?cpane=about\&quote;>About Us</a>&quote;;
//etc...

//center section
if(isset($_GETї'cpane'])){
   switch($_GETї'cpane']){
     case 'product':
     include(&quote;product.php&quote;);
     break;
     case 'about':
     include(&quote;about_us.php&quote;);
     break;
     //etc
   }//end switch
}else{ // end if for cpane set
   include(&quote;default.php&quote;);
} // end else for cpane set
as I was writing that I decided that's not the way I'd do it, but at least it will get you thinking of ways that you can do it. As I said, your best bet is going to be to do a search on this forum for other options....

Posted: Thu Jun 30, 2005 12:33 pm
by CutAndPaste
Burrito,

Thanks for that, I'll give it a go, I'd been searching on "Menu" and "Menu Includes", I'll try "navigation" now as well!

thanks...

Posted: Thu Jun 30, 2005 3:05 pm
by CutAndPaste
Burrito,

In my test page (menu.php) I've got

Code: Select all

<?php 
include "includes-menu/leftnav.php";
?>
in the left hand area

In the central area I've got:

Code: Select all

<?php 
include "includes-menu/centralpane.php";
?>
I've also created a load of dummy pages to correspond to my top level menu choices fruit.php, colours.php & sport.php

I've editted your suggested code and have got:

Code: Select all

<?php
//left nav section
echo "<a href=\"index.php?cpane=fruit\">Fruit</a>";
if(isset($_GET['pane']) && $_GET['pane'] == 'fruit'){
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=fruit&prod=apples\">Apples</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=fruit&prod=pears\">Pears</a>";
} 
// end if for fruit
?>
<br>
<?php
echo "<a href=\"index.php?cpane=colour\">Colours</a>";
if(isset($_GET['pane']) && $_GET['pane'] == 'colours'){
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=colours&prod=red\">Red</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=colours&prod=green\">Green</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=colours&prod=blue\">Blue</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=colours&prod=yellow\">Yellow</a>";
} 
// end if for colours
?>

<br>
<?php
echo "<a href=\"index.php?cpane=sport\">Sports</a>";
if(isset($_GET['pane']) && $_GET['pane'] == 'sports'){
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=sports&prod=running\">Running</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=sports&prod=jumping\">Jumping</a>";
   echo "&nbsp;&nbsp;<a href=\"index.php?cpane=sports&prod=swimming\">Swimming</a>";
   } 
// end if for sports
?>
<br>    
<?php
echo "<a href=\"index.php?cpane=about\">About Us</a>";
?>
<br>
<?php
echo "<a href=\"index.php?cpane=contact\">Contact Us</a>";
?>
I've created centralpane.php, and it contains this:

Code: Select all

<?php
//center section
if(isset($_GET['cpane'])){
   switch($_GET['cpane']){
     case 'fruit':
     include("/includes-menu/fruit.php");
     break;
	 //fruit-submenu
	 case 'apples':
     include("/includes-menu/apples.php");
     break;
	 case 'pears':
     include("/includes-menu/pears.php");
     break;
     //colours-main
	 case 'colours':
     include("includes-menu/colours.php");
     break;
	 case 'sport':
     include("includes-menu/sport.php");
     break;
     //etc
   }//end switch
}else{ // end if for cpane set
   include("404.php");
} // end else for cpane set
?>
My dummy pages just contain (for example)

Code: Select all

<php?
Fruit
?>
When open my test page (mydomain.menutest.php), I get the 404.php page displayed in the central area. When I click on one of the links say Fruit, I get the site's index.php showing and none of the sub menu items expand to show the fruit types. In the address bar it shows mydomain.com/index.php?cpane=fruit

Have I done something to break your code?

thanks,

Posted: Thu Jun 30, 2005 3:20 pm
by Burrito
you need to change $_GET['pane'] to $_GET['cpane'] (I prolly just had a typo this morning) on your nav stuff.

as for the 404, that's really odd...if it can't find the file to include, php should throw an error and not show anything :?

try fixing the nav section and see what happens...do you have a link so I can see this?

Posted: Thu Jun 30, 2005 4:44 pm
by CutAndPaste
Burrito,

I made the changes you suggested AND corrected my typo (the page is actually on menutest.php NOT index.php so that part of it works ok now).

The menu links come out as required, but I think I've still got soemthing wrong.
Typing mydomain.com/menutest.php dispalys the 404.php (this is a simple php page where I've written typed 404, - it's NOT a standard "can't be found 404" page).

Whenever I click on a link now, I just get an empty space where the contnet is supposed to appear.

I was wondering what the prod was for/from in:

Code: Select all

echo "&nbsp;&nbsp;<a href=\"menutest.php?cpane=fruit&prod=apples\">Apples</a>";
Also, to complicate matters, I'd like a 3rd menu level. I've tried this:

Code: Select all

<?php
//left nav section
echo "<a href=\"menutest.php?cpane=fruit\">Fruit</a>";
if(isset($_GET['cpane']) && $_GET['cpane'] == 'fruit'){
   ?><br><br>
   <?php
   echo "&nbsp;&nbsp;<a href=\"menutest.php?cpane=fruit&prod=apples\">Apples</a>";
       if(isset($_GET['cpane']) && $_GET['cpane'] == 'apples'){
       echo "&nbsp;&nbsp;<a href=\"menutest.php?cpane=fruit&prod=bramley\">Cooking Apple</a>";
       echo "&nbsp;&nbsp;<a href=\"menutest.php?cpane=fruit&prod=cox\">Eating Apple</a>";
       } //end if for Apples
   echo "&nbsp;&nbsp;<a href=\"menutest.php?cpane=fruit&prod=pears\">Pears</a>";
   }
// end if for fruit
 ?>
but the third level (Eating Apple & Cooking Apple) doesn't show. Never was very good at recursive if statements!

This is so nearly there now!

Thanks for you help. I really appreciate it!

Posted: Thu Jun 30, 2005 5:01 pm
by Burrito
prod would be the page that needs to now be loaded in the center section. You still need to keep the cpane var though to show the extended parents on the nav bar (at least the way I've done it).

as I said, there a ton of ways to do it, and I just whipped that up this morning. As I also said, it's probably not the way I'd do it and I apologize for sending you down that path. My example was more to get you thinking about ways of doing it rather than a "snippet" for you to use.

now for how'd I'd REALLY do it:

I would create a header page (header.inc.php) that contained all of your nav stuff from another file (nav.inc.php). I'd then include header.inc.php on all of my pages but load the pages separately vs including them in index.php. Finally I'd create a footer.inc.php to include on all of those same pages.

the good thing about this schema is you can very easily change your layout by just modifying header.inc.php, or add new nav by modifying nav.inc.php and it's all done in one location...no need to adjust 50 different pages when you want to make a change.

to adjust the nav (expand the "menu"), I would set vars above my include of header.inc.php to tell it which page it's on then expand accordingly.

ex:

Code: Select all

$page = &quote;fruit&quote;;
include(&quote;header.inc.php&quote;);

Code: Select all

echo &quote;&lt;table width=\&quote;100\&quote;&gt;&lt;tr&gt;&lt;td&gt;&quote;;
//yadda yadda
include(&quote;nav.inc.php&quote;);
// etc

Code: Select all

echo &quote;&lt;a href=\&quote;fruit.php\&quote;&gt;fruit&lt;/a&gt;&quote;;
if($page == &quote;fruit&quote;){
  echo &quote;&amp;nbsp;&amp;nbsp;&lt;a href=\&quote;apples.php\&quote;&gt;apples&lt;/a&gt;&quote;;
  echo &quote;&amp;nbsp;&amp;nbsp;&lt;a href=\&quote;pears.php\&quote;&gt;pears&lt;/a&gt;&quote;;
}
//etc etc
again I apologize for misdirecting you initially, I just wanted to get you thinking a wee bit. Hopefully this will make more sense and is a much better paradigm for doing what you're after...