Jump to content

Navigation include for all site directories


amelio

Recommended Posts

Hi,

 

I need a navigation include that can deal with directories at different levels on a site. All I can seem to find are the basic includes for files all in the same directory. This doesn't work for me because as soon as you navigate to a page in a nested directory the relative links don't work.

 

Is the only option for me to use absolute links? This would cause difficulty as I would need different addresses to achieve the same result on the testing server and remote site.

 

I also need to be able to identify the current page to style the link. At the moment I am using the code below which is fine whilst everything is in the same directory but introduce nested directories and some links will be reachable with just the $base. But only as long as current page and destination page are in the same directory. 

 

$base=basename($_SERVER['PHP_SELF']);
$menu=preg_replace("|<li><a href=\"".$base."\">(.*)</a></li>|U", "<li class=\"current\">$1</li>", $menu);
echo $menu;

 

I would greatly appreciate your help.

 

 

Link to comment
Share on other sites

 

Thanks abazoskib,

 

Using your example. If I am in Directory B, ../test.txt allows me to get to test.txt in Directory A but if I am navigating to test.txt from within Directory A I would only need test.txt & not ../test.txt. This is my problem, how do I accomodate these multiple relative addresses within one include file? This example shows just two but in a larger site you might have multiple nested directories with many files named index.php.

 

 

Link to comment
Share on other sites

Well then your best bet would be to define a constant variable in a file called global_settings.php for example such as

 

define("INDEX_PAGE","/path/to/file/index.php")

 

Now, if you include that global file in all your pages using an absolute path to the global file, you can simply use INDEX_PAGE to include the index page.

You can put all your constant definitions within that global file, so you will only have to edit that if your structure ever changes.

Link to comment
Share on other sites

I sat down for a good half hour trying to get my head around your solution abazoskib but to no avail. I'm sure this is a reflection on my lack of understanding rather than your explanation. I would need to see an example to get it.

 

I have come up with a solution which allows me to include the same file in every page on the site but I just have to use an absolute path to that include file. So not fully satisfactory.

 

I do the test because the site on the testing server is in a folder below the root.

 

if ($_SERVER['SERVER_NAME'] == "localhost") {
$testing = "/testing_folder";
}
else {
$testing = "";
};

echo "<ul id=\"main_nav\"> 
<li><a href=\"".$testing."/index.php\">Home</a></li> 
<li><a href=\"".$testing."/welcome.php\">Welcome</a></li> 
<li><a href=\"".$testing."/emails.php\">Emails</a></li> 
<li><a href=\"".$testing."/news/index.php\">Village News</a></li> 
</ul>";

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

global_settings.php

<?php
define("INDEX_PAGE","http://yoursite.com/absolute/path/to/file");
define("WELCOME_PAGE","http://yoursite.com/absolute/path/to/file");
define("EMAILS_PAGE","http://yoursite.com/absolute/path/to/file");
define("NEWS_INDEX_PAGE","http://yoursite.com/absolute/path/to/file");
?>

 

The above uses "http://yoursite.com/absolute/path/to/file", just replace it with the actual full path to each file.

Now in any file where you need to include these  as links, use like so:

<?php
require_once('global_settings.php'); //use the absolute server path to this file
echo "<ul id=\"main_nav\">
<li><a href=\"".INDEX_PAGE."\">Home</a></li>
<li><a href=\"".WELCOME_PAGE."\">Welcome</a></li>
<li><a href=\"".EMAILS_PAGE."\">Emails</a></li>
<li><a href=\"".NEWS_INDEX_PAGE."\">Village News</a></li>
</ul>";
?>

 

The advantage to this is that all your links will be defined within one file, so if anything changes you can always update one single file, instead of having to update every single page.

 

 

 

Link to comment
Share on other sites

Thanks for that abazoskib,

 

I will play around. I am hoping that by including the directory name in the constant as you have done, that will make it possible for me to identify the current page link so that I can style that. I will post back in a few days.

 

Appreciate the time you spent to help me.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.