Jump to content

Php Navigation


holmedwa04
 Share

Recommended Posts

Hey EveryoneI have a website and it currently uses PHP includes so that the header, footer, navigation and right pane can be edited and shown on any page. This makes life somewhat easier. But I was wondering if it was possible to get it to only display the pages you were not on...so it looks at the page you are on and then removes that from the navigation include? So if there were 4 pages... page one would have 2,3,4 page two 1,3,4 page three 1,2,4 page four 1,2,3.Any help would be greatly appreciated. Thanks in advance.N.B. I am a complete novice with PHP and have only used include functions so far!

Edited by holmedwa04
Link to comment
Share on other sites

You could just check what page you are on (I suppose it's stored in the querystring) and not display that one.Do you have any code you could show us?

Link to comment
Share on other sites

depending on how the menu is produced, but you could assign a specific class or id to each button fro example:<div class="Button but1"><a href="#">Home</a></div><div class="Button but2"><a href="#">Abouts Us</a></div><div class="Button but3"><a href="#">Hobbies</a></div><div class="Button but4"><a href="#">Other</a></div>then use css in that page to hide <style type="text/css">.but2 {display:none;}</style>

Link to comment
Share on other sites

This is the code that is in the navigation include file...

<ul id="nav"><li>Navigation</ul><ul id="menu"><li><a class="nav" href="index.php">Home</a><li><a class="nav" href="lights.php">How to Make Lights</a><li><a class="nav" href="archive.php">News Archive</a><li><a class="nav" href="about.php">About the Layout</a><li><a class="nav" href="layout.php">Track Plan</a><li><a class="nav" href="news.php">Layout News</a></ul>

Link to comment
Share on other sites

you have no closing </li> tags ? anyway assign specific id or class to li tags<ul id="nav"><li>Navigation</li></ul><ul id="menu"><li class="but1"><a class="nav" href="index.php">Home</a></li><li class="but2"><a class="nav" href="lights.php">How to Make Lights</a></li><li class="but3"><a class="nav" href="archive.php">News Archive</a></li><li class="but4"><a class="nav" href="about.php">About the Layout</a></li><li class="but5"><a class="nav" href="layout.php">Track Plan</a></li><li class="but6"><a class="nav" href="news.php">Layout News</a></li></ul>insert css into page which one you wish to hide.

Link to comment
Share on other sites

You can get the filename of the current page like this:

$fname = basename($_SERVER['SCRIPT_NAME']);

You can compare that before you print each menu item.

<?php if ($fname != 'index.php') { ?><li><a class="nav" href="index.php">Home</a></li><?php } ?>

or

<?php if ($fname != 'index.php') echo '<li><a class="nav" href="index.php">Home</a></li>'; ?>

Link to comment
Share on other sites

Thanks justsomeguy.... I have tried it with this code:

<ul id="nav"><li>Navigation</ul><ul id="menu"><?php if ($fname != 'index.php') echo '<li><a class="nav" href="index.php">Home</a></li>'; ?><?php if ($fname != 'lights.php') echo '<li><a class="nav" href="lights.php">How to Make Lights</a></li>'; ?><?php if ($fname != 'archive.php') echo '<li><a class="nav" href="archive.php">News Archive</a></li>'; ?><?php if ($fname != 'about.php') echo '<li><a class="nav" href="about.php">About the Layout</a></li>'; ?><?php if ($fname != 'layout.php') echo '<li><a class="nav" href="layout.php">Track Plan</a></li>'; ?><?php if ($fname != 'news.php') echo '<li><a class="nav" href="news.php">Layout News</a></li>'; ?></ul>

But with no luck... is there something that I am missing?

Link to comment
Share on other sites

try using an array and do a compare of links<?php $fname = basename($_SERVER['SCRIPT_NAME']);$nav= array();$nav[0]=array("index.php","Home");$nav[1]=array("lights.php","How to Make Lights");$nav[2]=array("archive.php","News Archive");$nav[3]=array("about.php","About the Layout");$nav[4]=array("layout.php","Track Plan");$nav[5]=array("news.php","Layout News");echo ("<ul id=\"nav\">");echo ("<li>Navigation</li>");echo ("</ul>");echo ("<ul id=\"menu\">");for ($i=0;$i<count($nav);$i++){if($fname != $nav[$i][0]){echo("<li><a class=\"nav\" href=\"".$nav[$i][0]."\">".$nav[$i][1]."</a></li>");}}echo ("</ul>");?>Sorry realised you did not need styling with this method

Edited by dsonesuk
Link to comment
Share on other sites

When I put this code in:

$fname = basename($_SERVER['SCRIPT_NAME']);

It just shows up where ever I place it and doesn't actually do anything. Please could someone write out the code that I need? I have very little experience of PHP and so struggle to know where anything goes. Sorry to be so much bother.

Link to comment
Share on other sites

on the page you want to insert the nav llink you include ref to the nav pageanypage.php<?phpinclude("nav.php");?>and insert the function to retrieve nav links within the body<?phpshownav();?>nav.php insert this into a completly blank page<?phpfunction shownav(){$fname = basename($_SERVER['SCRIPT_NAME']);$nav= array();$nav[0]=array("index.php","Home");$nav[1]=array("lights.php","How to Make Lights");$nav[2]=array("archive.php","News Archive");$nav[3]=array("about.php","About the Layout");$nav[4]=array("layout.php","Track Plan");$nav[5]=array("news.php","Layout News");echo ("<ul id=\"nav\">");echo ("<li>Navigation</li>");echo ("</ul>");echo ("<ul id=\"menu\">");for ($i=0;$i<count($nav);$i++){if($fname != $nav[$i][0]){echo("<li><a class=\"nav\" href=\"".$nav[$i][0]."\">".$nav[$i][1]."</a></li>");}}echo ("</ul>");}?>and thats it

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...